1 / 13

Engineering Problem Solving With C++ An Object Based Approach

Engineering Problem Solving With C++ An Object Based Approach. Chapter 4 Programming with Data Files. Object Oriented Programming. A class is a mechanism that allows a programmer to define new data types.

montana
Download Presentation

Engineering Problem Solving With C++ An Object Based Approach

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 objectis a variable of a defined class type, also referred to as an instanceof a class.

  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()

  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()

  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

  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()

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

  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

  9. #include <fstream> #include <cmath> 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

  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

  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; … } …

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

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

More Related