Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.

Similar presentations


Presentation on theme: "1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files."— Presentation transcript:

1

2 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files

3 2 Object Oriented Programming A class is a mechanism that allows a programmer to define new data types. A class can be used to add functionality to an existing data type or to create a new data type. A class definition combines data and functionality. An object is a variable of a defined class type, also referred to as an instance of a class.

4 3 File Streams cin –object of type istream –istream class is defined in file iostream –cin is also declared in iostream (istream cin;) defined to stream input from standard input some associated member functions: –eof() –get()

5 4 File Streams cout –object of type ostream –ostream is defined in the file iostream –cout is also declared in iostream (ostream cout;) cout is defined to stream output to standard output some associated member functions: –put()

6 5 Programmer Defined File Streams To define your own file streams – for input use ifstream class – for output use ofstream class ifstream and ofstream are defined in file fstream

7 6 File Streams ifstream is derived from istream includes all functions defined for cin - eof(), get(),.. Includes additional functions not defined for cin - open(), close()

8 7 File Streams ofstream is derived from ostream includes all functions defined for cout - put(),.. Includes additional functions not defined for cout - open(), close()

9 8 Defining File Streams Include fstream declare file stream variable (object) –ifstream fin; –ofstream fout; use open() to initialize file stream variable use file stream variable as you would use cin and cout use close() to close the file when finished with it

10 9 #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 plotdata.open("plot1");//plotdata uses 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

11 10 Reading Data Files Specified number of records for loop Trailer or Sentinel Signal while loop Data records only (no specified number of records, no sentinel value) while loop

12 11 Example - Specified Number of Records … int main() { fstream fin(“data”); double x; int num_data_points; … fin >> num_data_points; for(int i=0; i<num_data_points; i++) { fin >> x; … } …

13 12 Example - Sentinel Signal const double sentinelValue = -99; … int main() { fstream fin(“data”); double x; … fin >> x; while(x != sentinelValue) { … fin >> x; } …

14 13 Example - No Specified Number of Records, no Sentinel Signal … int main() { fstream fin(“data”); double x; … fin >> x; while( !fin.eof() ) { … fin >> x; } …


Download ppt "1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files."

Similar presentations


Ads by Google