140 likes | 248 Views
Declaring fstream Objects. An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen. These streams are constructed automatically. Declaring fstream Objects.
E N D
Declaring fstream Objects • An istream object named cin connects program and keyboard • An ostream object named cout connects the program and the screen These streams are constructed automatically.
Declaring fstream Objects • For doing I/O from/to a file a program must explicitly open a stream • Creates a connection between a program in memory and a text file
Basic fstream Operations • Must use #include <fstream> • open() Establishes connection program to file • is_open() Returns true/false • >> Operator, inputs value from file • getline() Reads line of text into string object • << Operator, outputs value to file • eof() Returns true/false, end of file • close() Terminates connection between program, file
The open() Operation • Given:ifstream inStream;inStream.open( "pressure.dat"); • Parameter can also be a variable • If it is a string variable ( string fileName ) must use fileName.data() for correct parameter type • When input file is opened, read position pointer set to beginning of sequence of characters in the file
The open() Operation (for output) • When output file is opened, file is created on the disk, with write-position pointer pointing at the eof marker • Opening an ofstream to a file will create a new file • If file existed before, it is now (by default) destroyed • Otherwise, new file is created
The open() Operation (omit) • Possible to open the file with a mode argument as a second parameter
Initialization at Declaration • Possible to open at declaration of variableofstream outStream ("pressure.out");ifstream inStream ("pressure.in"); ExecutingProgram
Programming Defensively • The success or failure of a call to open a file should always be tested • Use inStream.open() • Us in an assert( ) mechanism • Call before proceeding with additional operations on the file
The Input Operator • We have used cin >> x; • Value entered via the keyboard • C++ uses the same operator to bring values into variables from a streaminStream >> reading; • The reading pointer keeps track of where in the stream the program is currently reading
The getline() Function • Requires an istream object, a string objectgetline (nameStream, name); • Reads entire name into variable • Reads until it hits a newline character • Newline character read, not added to variable Note: the >> operator does not read the newline. The next >> skips it as white space. But if a getline is used next, it sees the newline and terminates. Think about what happens if you mix >> and getline calls.
The eof() Message • Can be used as a sentinel value to control an input loopfor ( ; ; ) { inStream >> reading; if (inStream.eof() ) break; // . . . process the input } • inStream.eof() returns true following execution of the input statement at this point
The Output Operator << • Overloaded to perform with ostream, ofstream objectsoutStream <<"\n--> There were a total of" << count << "values."; • Note that the write pointer is pushed forward, keeps pointing at the eof marker.
The close() Message • The file stream is disconnected when • The program leaves the scope of the fstream object (implicitly) • The close() message is executed (explicitly) • It is good practice to explicitly close a file when the program is done using it • If many files are accessed, the operating system may place a limit on how many files are open simultaneously