Download presentation
Presentation is loading. Please wait.
1
Programming with Data Files
ELEC 206 Computer Tools for Electrical Engineering
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.
3
Simple I/O: cin object of type istream
istream class is defined in file iostream cin is also declared in iostream (istream cin;) stream input from standard input using the extraction operator (cin >> identifier) Data entered from the keyboard must be compatible with the data type of the variable. some associated member functions: eof(), get()
4
Simple I/O: 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 using the insertion operator (cout << expression << expression;) an expression is any C++ expression (string constant, identifier, formula or function call) some associated member functions: put()
5
Characters and input >> discards leading whitespace
get() method used to input whitespace characters Example: int x; char y; cin >> x >> y; cin >> x; cin.get(y); x y x y 45 c 39 b 45 ‘c’ 39 ‘\n ’
6
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
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
File Streams ofstream is derived from ostream
includes all functions defined for cout - put(), .. Includes additional functions not defined for cout - open(), close()
9
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
#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(); } //end main
11
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)
12
Example - Specified # 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
Example - Sentinel Signal
const double sentinelValue = -99; … int main() { fstream fin(“data”); double x; fin >> x; while(x != sentinelValue) }
14
Example - No Specified # of Records, no Sentinel Signal
… int main() { fstream fin(“data”); double x; fin >> x; while( !fin.eof() ) }
15
Stringstream stringstream class is defined in file sstream
provides an interface to manipulate strings as if they were input/output streams. The object of this class can be identified with iostream some associated member functions: get(), getline(), ignore(), putback(), etc. Member functions can be inherited from the iostream class Useful for converting from one arbitrary type to another
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.