Presentation is loading. Please wait.

Presentation is loading. Please wait.

Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.

Similar presentations


Presentation on theme: "Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These."— 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 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 ModeDescription ios::in open for input, non destructive, read pointer at beginning ios::trunc Open file, delete contents it contains ios::out Open for output, use ios::trunc ios::app Open for output, nondestructive, write position at end of file ios::ate Open existing file with read (or write) position at end of file ios::binary Open file in binary mode

7 Initialization at Declaration Possible to open at declaration of variable ofstream outStream ("pressure.out"); ifstream inStream ("pressure.in"); Executing Program

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 stream inStream >> 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 object getline (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. 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 Can be used as a sentinel value to control an input loop for ( ; ; ) { inStream >> reading; if (inStream.eof() ) break; //... process the input } inStream.eof() returns true following execution of the input statement at this point The eof() Message

12 The Output Operator << Overloaded to perform with ostream, ofstream objects outStream 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


Download ppt "Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These."

Similar presentations


Ads by Google