Download presentation
Presentation is loading. Please wait.
Published byOpal Pitts Modified over 9 years ago
2
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files
3
4 Defining File Streams 4 Reading and Generating Data Files 4 Error Checking Engineering Problem Solving with C++, Second Edition, J. Ingber 2
4
DEFINING FILE STREAMS stream class hierarcy Engineering Problem Solving with C++, Second Edition, J. Ingber 3
5
Stream Class Hierarchy 4 C++ supports data abstraction and object oriented programming through the use of class hierarchies. 4 The stream class hierarchy is defined in the C++ standard library Engineering Problem Solving with C++, Second Edition, J. Ingber 4
6
The C++ Stream Class Hierarchy ios_baseiosistreamifstreamiostreamostreamofstream Engineering Problem Solving with C++, Second Edition, J. Ingber 5
7
Stream Class Hierarchy istream class supports standard input. ostream class supports standard output. ifstream class supports file input. ofstream class supports file output. Engineering Problem Solving with C++, Second Edition, J. Ingber 6
8
Standard Stream Objects 4 Standard stream objects are initialized by the compiler to stream data to the system's standard input and output devices. –cin –cout –cerr Engineering Problem Solving with C++, Second Edition, J. Ingber 7
9
8 Standard Input Device cin –object of type istream cin is defined by the compiler to stream input from standard input. some associated member functions: –eof () –get ()
10
Engineering Problem Solving with C++, Second Edition, J. Ingber 9 Standard Output Device cout –object of type ostream cout is defined by the compiler to stream output to standard output some associated member functions: –put ()
11
Engineering Problem Solving with C++, Second Edition, J. Ingber 10 Standard Error Device cerr –object of type ostream cerr is defined by the compiler to stream output to standard error cerr is an unbuffered stream.
12
Engineering Problem Solving with C++, Second Edition, J. Ingber 11 Programmer Defined File Streams 4 To define an input file stream: – use the ifstream class 4 To define an output file stream – use the ofstream class ifstream and ofstream are defined in header file fstream.
13
Engineering Problem Solving with C++, Second Edition, J. Ingber 12 File Streams ifstream class is derived from istream –inherits >>,eof (), get (),.. Includes additional functions not defined in istream – open (), close ()
14
Engineering Problem Solving with C++, Second Edition, J. Ingber 13 File Streams ofstream class is derived from ostream –inherits <<, put(),.. Includes additional functions not defined in ostream – open(), close()
15
READING AND CREATING DATA FILES plotting data Engineering Problem Solving with C++, Second Edition, J. Ingber 14
16
Engineering Problem Solving with C++, Second Edition, J. Ingber 15 Defining File Streams Include fstream. 4 Declare file stream objects: –ifstream fin; –ofstream fout; Use open () to associate a data file with a file stream object. Use close() to close the file when finished.
17
Engineering Problem Solving with C++, Second Edition, J. Ingber 16 #include //required for cout #include //required for ifstream,ofstream #include using namespace std; int main() { //Create three columns of data for plotting. double x; ifstream xdata; //declare input stream ofstream plotdata; //declare output stream xdata.open("data1"); //xdata uses file data1 if( xdata.fail() ) //check for error { cout << "error opening input file" << endl; return 0; } //end if fail
18
Engineering Problem Solving with C++, Second Edition, J. Ingber 17 plotdata.open("plot1"); //output file plot1 xdata >> x; //input x from file while(!xdata.eof()) //while not end of file { if( x>0 ) //write to plot file plotdata << x <<”,” <<exp(x) << "” << log(x) << endl; xdata >> x; //get next data point } //end while xdata.close(); plotdata.close(); return 0; } //end main
19
ERROR CHECKING the stream state Engineering Problem Solving with C++, Second Edition, J. Ingber 18
20
Error Checking 4 Errors can occur when inputting data from a file or from standard input: –no data is available (end of file). –type of data is incompatible with type of input variable. 4 When an error occurs while attempting to use a stream object, the object places itself in an error state. Engineering Problem Solving with C++, Second Edition, J. Ingber 19
21
The Stream State 4 Every stream has an associated state. The state of a stream is represented as a set of attributes, defined in the ios_base class. –badbit –failbit –eofbit –goodbit Engineering Problem Solving with C++, Second Edition, J. Ingber 20
22
Stream Class Funtions 4 The state of a stream can be tested and modified at run time using member functions defined in the stream class hierarchy. –bool bad() –bool eof() –bool fail() –bool good() –void clear() –iostate rdstate() Engineering Problem Solving with C++, Second Edition, J. Ingber 21
23
Example: //include necessary headers //… ifstream fin; int x; fin.open(data.txt); //open input file if(fin.fail()) { cerr << "error opening file " << endl; } fin >> x; Engineering Problem Solving with C++, Second Edition, J. Ingber 22 //while fin is not in an error state while(fin) { cout << x << endl; fin >> x; } //Print state of fin to standard output cout << "state of fin stream: " << endl << fin.bad() << "," << fin. eof() << "," << fin.fail() << "," << fin.good() << endl; return 0; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.