Chapter 10 Input/Output Streams

Slides:



Advertisements
Similar presentations
Slide: 1 Interra Induction Training Object Oriented Programming in C++ IO Streams Kolkata, July 25, 2005.
Advertisements

I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Chapter 3 Objects, types, and values Bjarne Stroustrup
Chapter 11 Customizing I/O Bjarne Stroustrup
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Chapter 24 Numerics Bjarne Stroustrup
Chapter 4 Computation Bjarne Stroustrup
Chapter 9 Technicalities: Classes, etc. Bjarne Stroustrup
Chapter 6 Writing a Program
Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers.
Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.
Chapter 20 The STL (containers, iterators, and algorithms) John Keyser’s Modifications of Slides by Bjarne Stroustrup
1 Lecture 31 Handling Selected Topics Again!! File I/O Special Lectures.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 3: Input/Output
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Chapter 11 Customizing I/O Bjarne Stroustrup
Chapter 10 Input/Output Streams John Keyser’s Modifications of Slides by Bjarne Stroustrup
C++ Lecture 8 Monday, 25 August I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
1 CS161 Introduction to Computer Science Topic #13.
FILE HANDLING IN C++.
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
Slides adapted from: Bjarne Stroustrup, Programming – Principles and Practice using C++ Chapter 10 Input/Output Streams Hartmut Kaiser
FILE I/O IN C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples.
Chapter 3: Input/Output
1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
1 CSC241: Object Oriented Programming Lecture No 32.
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring Fall 2016 Set 10: Input/Output Streams 1 Based on slides created.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
Files To read a file – We must know its name – We must open it (for reading) – Then we can read – Then we must close it That is typically done implicitly.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
Slides adapted from: Bjarne Stroustrup, Programming – Principles and Practice using C++ Chapter 10 Input/Output Streams Hartmut Kaiser
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management Concept of streams. cin and cout objects. C++stream classes. Unformatted I/O.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
CS212: Object Oriented Analysis and Design
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
Basic Input and Output Operations
CSCI 161: Introduction to Programming
Binary Files.
Chapter 10 Input/Output Streams
Basic File I/O and Stream Objects
IO Overview CSCE 121 J. Michael Moore
files Dr. Bhargavi Goswami Department of Computer Science
Chapter 3: Input/Output
Chapter 10 Input/Output Streams
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
IO Overview CSCE 121 Strongly influenced by slides created by Bjarne Stroustrup and Jennifer Welch.
Lecture 9 Files Handling
File I/O in C++ I.
Introduction to Programming
Input/Output Streams, Part 2
Presentation transcript:

Chapter 10 Input/Output Streams Bjarne Stroustrup www.stroustrup.com/Programming

Abstract We get data from files, sensors, web connections, etc., which we want to analyze, print, graph, etc. Sometimes, we want to produce such data. In this lecture, we look at C++’s basic mechanisms for reading and writing streams of data. We also discuss an interesting – apparently trivial – problem: how to read an integer. Stroustrup/Programming -- Oct'10

Overview Fundamental I/O concepts Files I/O errors Opening Reading and writing streams I/O errors Reading a single integer Stroustrup/Programming -- Oct'10

Input and Output our program input device device driver input library output library output device data source: data destination: Stroustrup/Programming -- Oct'10

The stream model c “somewhere” ostream (1,234) 123 buffer An ostream turns values of various types into character sequences sends those characters somewhere E.g., console, file, main memory, another computer Stroustrup/Programming -- Oct'10

The stream model c “somewhere” istream (1,234) 123 buffer An istream turns character sequences into values of various types gets those characters from somewhere E.g., console, file, main memory, another computer Stroustrup/Programming -- Oct'10

The stream model Reading and writing Of typed entities << (output) and >> (input) plus other operations Type safe Formatted Typically stored (entered, printed, etc.) as text But not necessarily (see binary streams in chapter 11) Extensible You can define your own I/O operations for your own types A stream can be attached to any I/O or storage device Stroustrup/Programming -- Oct'10

Files We turn our computers on and off We like to keep our data The contents of our main memory is transient We like to keep our data So we keep what we want to preserve on disks and similar permanent storage A file is a sequence of bytes stored in permanent storage A file has a name The data on a file has a format We can read/write a file if we know its name and format Stroustrup/Programming -- Oct'10

A file 0: 1: 2: At the fundamental level, a file is a sequence of bytes numbered from 0 upwards Other notions can be supplied by programs that interpret a “file format” For example, the 6 bytes "123.45" might be interpreted as the floating-point number 123.45 Stroustrup/Programming -- Oct'10

Files General model disk Main memory I/O system iostreams Objects (of various types) Files (sequences of bytes) Stroustrup/Programming -- Oct'10

Files To read a file To write a file We must know its name We must open it (for reading) Then we can read Then we must close it That is typically done implicitly To write a file We must name it We must open it (for writing) Or create a new file of that name Then we can write it We must close it Stroustrup/Programming -- Oct'10

Opening a file for reading // … int main() { cout << "Please enter input file name: "; string name; cin >> name; ifstream ist(name.c_str()); // ifstream is an“input stream from a file” // c_str() gives a low-level (“system” // or C-style) string from a C++ string // defining an ifstream with a name string // opens the file of that name for reading if (!ist) error("can’t open input file ", name); Stroustrup/Programming -- Oct'10

Opening a file for writing // … cout << "Please enter name of output file: "; cin >> name; ofstream ofs(name.c_str()); // ofstream is an “output stream from a file” // defining an ofstream with a name string // opens the file with that name for writing if (!ofs) error("can’t open output file ", name); } Stroustrup/Programming -- Oct'10

Remember Sometimes students want to read to a file or write from a file – this causes errors We read in from an input stream (ist >>) We write out to an output stream (ost <<) It’s like a piece of paper: Reading is getting information from the paper Writing is putting information on the paper Stroustrup/Programming -- Oct'10

Reading from a file Suppose a file contains a sequence of pairs representing hours and temperature readings 0 60.7 1 60.6 2 60.3 3 59.22 The hours are numbered 0..23 No further format is assumed Maybe we can do better than that (but not just now) Termination Reaching the end of file terminates the read Anything unexpected in the file terminates the read E.g., q Stroustrup/Programming -- Oct'10

Reading a file struct Reading { // a temperature reading int hour; // hour after midnight [0:23] double temperature; Reading(int h, double t) :hour(h), temperature(t) { } }; vector<Reading> temps; // create a vector to store the readings int hour; while (ist >> hour >> temperature) { // read if (hour < 0 || 23 <hour) error("hour out of range"); // check temps.push_back( Reading(hour,temperature) ); // store } Stroustrup/Programming -- Oct'10

I/O error handling Sources of errors Human mistakes Files that fail to meet specifications Specifications that fail to match reality Programmer errors Etc. iostream reduces all errors to one of four states good() // the operation succeeded eof() // we hit the end of input (“end of file”) fail() // something unexpected happened bad() // something unexpected and serious happened Stroustrup/Programming -- Oct'10

Sample integer read “failure” Ended by “terminator character” 1 2 3 4 5 * State is fail() Ended by format error 1 2 3 4 5.6 Ended by “end of file” 1 2 3 4 5 end of file 1 2 3 4 5 Control-Z (Windows) 1 2 3 4 5 Control-D (Unix) State is eof() Something really bad Disk format error State is bad() Stroustrup/Programming -- Oct'10

I/O error handling void fill_vector(istream& ist, vector<int>& v, char terminator) { // read integers from ist into v until we reach eof() or terminator int i = 0; while (ist >> i) v.push_back(i); // read and store in v until “some failure” if (ist.eof()) return; // fine: we found the end of file if (ist.bad()) error("ist is bad"); // stream corrupted; let’s get out of here! if (ist.fail()) { // clean up the mess as best we can and report the problem ist.clear(); // clear stream state, so that we can look for terminator char c; ist>>c; // read a character, hopefully terminator if (c != terminator) { // unexpected character ist.unget(); // put that character back ist.clear(ios_base::failbit); // set the state back to fail() } Stroustrup/Programming -- Oct'10

Throw an exception for bad() // How to make ist throw if it goes bad: ist.exceptions(ist.exceptions()|ios_base::badbit); // can be read as // “set ist’s exception mask to whatever it was plus badbit” // or as “throw an exception if the stream goes bad” Given that, we can simplify our input loops by no longer checking for bad Stroustrup/Programming -- Oct'10

Simplified input loop void fill_vector(istream& ist, vector<int>& v, char terminator) { // read integers from ist into v until we reach eof() or terminator int i = 0; while (ist >> i) v.push_back(i); if (ist.eof()) return; // fine: we found the end of file // not good() and not bad() and not eof(), ist must be fail() ist.clear(); // clear stream state char c; ist>>c; // read a character, hopefully terminator if (c != terminator) { // ouch: not the terminator, so we must fail ist.unget(); // maybe my caller can use that character ist.clear(ios_base::failbit); // set the state back to fail() } Stroustrup/Programming -- Oct'10

Reading a single value Three kinds of problems are possible // first simple and flawed attempt: cout << "Please enter an integer in the range 1 to 10 (inclusive):\n"; int n = 0; while (cin>>n) { // read if (1<=n && n<=10) break; // check range cout << "Sorry, " << n << " is not in the [1:10] range; please try again\n"; } Three kinds of problems are possible the user types an out-of-range value getting no value (end of file) the user types something of the wrong type (here, not an integer) Stroustrup/Programming -- Oct'10

Reading a single value What do we want to do in those three cases? handle the problem in the code doing the read? throw an exception to let someone else handle the problem (potentially terminating the program)? ignore the problem? Reading a single value Is something we often do many times We want a solution that’s very simple to use Stroustrup/Programming -- Oct'10

Handle everything: What a mess! cout << "Please enter an integer in the range 1 to 10 (inclusive):\n"; int n = 0; while (n==0) { // Spot the bug! cin >> n; if (cin) { // we got an integer; now check it: if (1<=n && n<=10) break; cout << "Sorry, " << n << " is not in the [1:10] range; please try again\n"; } else if (cin.fail()) { // we found something that wasn’t an integer cin.clear(); // we’d like to look at the characters cout << "Sorry, that was not a number; please try again\n"; char ch; while (cin>>ch && !isdigit(ch)) ; // throw away non-digits if (!cin) error("no input"); // we didn’t find a digit: give up cin.unget(); // put the digit back, so that we can read the number else error("no input"); // eof or bad: give up // if we get here n is in [1:10] Stroustrup/Programming -- Oct'10

The mess: trying to do everything at once Problem: We have all mixed together reading values prompting the user for input writing error messages skipping past “bad” input characters testing the input against a range Solution: Split it up into logically separate parts Stroustrup/Programming -- Oct'10

What do we want? What logical parts do we what? int get_int(int low, int high); // read an int in [low..high] from cin int get_int(); // read an int from cin // so that we can check the range int void skip_to_int(); // we found some “garbage” character // so skip until we find an int Separate functions that do the logically separate actions Stroustrup/Programming -- Oct'10

Skip “garbage” void skip_to_int() { if (cin.fail()) { // we found something that wasn’t an integer cin.clear(); // we’d like to look at the characters char ch; while (cin>>ch) { // throw away non-digits if (isdigit(ch)) { cin.unget(); // put the digit back, // so that we can read the number return; } error("no input"); // eof or bad: give up Stroustrup/Programming -- Oct'10

Get (any) integer int get_int() { int n = 0; while (true) { if (cin >> n) return n; cout << "Sorry, that was not a number; please try again\n"; skip_to_int(); } Stroustrup/Programming -- Oct'10

Get integer in range int get_int(int low, int high) { cout << "Please enter an integer in the range " << low << " to " << high << " (inclusive):\n"; while (true) { int n = get_int(); if (low<=n && n<=high) return n; cout << "Sorry, " << n << " is not in the [" << low << ':' << high << "] range; please try again\n"; } Stroustrup/Programming -- Oct'10

Use Problem: The “dialog” is built into the read operations int n = get_int(1,10); cout << "n: " << n << endl; int m = get_int(2,300); cout << "m: " << m << endl; Problem: The “dialog” is built into the read operations Stroustrup/Programming -- Oct'10

What do we really want? int strength = get_int(1, 10, // parameterize by integer range and “dialog” int strength = get_int(1, 10, "enter strength", "Not in range, try again"); cout << "strength: " << strength << endl; int altitude = get_int(0, 50000, "please enter altitude in feet", "Not in range, please try again"); cout << "altitude: " << altitude << "ft. above sea level\n"; That’s often the really important question Ask it repeatedly during software development As you learn more about a problem and its solution, your answers improve Stroustrup/Programming -- Oct'10

Parameterize Incomplete parameterization: get_int() still “blabbers” int get_int(int low, int high, const string& greeting, const string& sorry) { cout << greeting << ": [" << low << ':' << high << "]\n"; while (true) { int n = get_int(); if (low<=n && n<=high) return n; cout << sorry << ": [" << low << ':' << high << "]\n"; } Incomplete parameterization: get_int() still “blabbers” “utility functions” should not produce their own error messages Serious library functions do not produce error messages at all They throw exceptions (possibly containing an error message) Stroustrup/Programming -- Oct'10

User-defined output: operator<<() Usually trivial ostream& operator<<(ostream& os, const Date& d) { return os << '(' << d.year() << ',' << d.month() << ',' << d.day() << ')'; } We often use several different ways of outputting a value Tastes for output layout and detail vary Stroustrup/Programming -- Oct'10

Use void do_some_printing(Date d1, Date d2) { cout << d1; // means operator<<(cout,d1) ; cout << d1 << d2; // means (cout << d1) << d2; // means (operator<<(cout,d1)) << d2; // means operator<<((operator<<(cout,d1)), d2) ; } Stroustrup/Programming -- Oct'10

User-defined input: operator>>() istream& operator>>(istream& is, Date& dd) // Read date in format: ( year , month , day ) { int y, d, m; char ch1, ch2, ch3, ch4; is >> ch1 >> y >> ch2 >> m >> ch3 >> d >> ch4; if (!is) return is; // we didn’t get our values, so just leave if (ch1!='(' || ch2!=',' || ch3!=',' || ch4!=')') { // oops: format error is.clear(ios_base::failbit); // something wrong: set state to fail() return is; // and leave } dd = Date(y,Month(m),d); // update dd return is; // and leave with is in the good() state Stroustrup/Programming -- Oct'10

Customizing input and output (chapter 11) Next Lecture Customizing input and output (chapter 11) Stroustrup/Programming -- Oct'10