1 / 13

Declaring fstream Objects

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.

Download Presentation

Declaring fstream Objects

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. 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.

  2. 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

  3. 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

  4. 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

  5. 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

  6. The open() Operation (omit) • Possible to open the file with a mode argument as a second parameter

  7. Initialization at Declaration • Possible to open at declaration of variableofstream outStream ("pressure.out");ifstream inStream ("pressure.in"); ExecutingProgram

  8. 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

  9. 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

  10. 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.

  11. 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

  12. 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.

  13. 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

More Related