IO File Classes


I/O File Classes

As mentioned above, there are three I/O file classes: ifstream, ofstream and fstream. In addition to using the constructors discussed above, these classes also have the method open( ) that can be used with the default constructors to initialize the I/O stream object. For example:

image from book

 void ifstream::open(const char*, ios::mode mode = ios::in, int protect = filebuf::openprot); 

image from book

The classes ofstream and fstream have a similar open( ). The modes for open( ) of ifstream and ofstream are the default values ios::in and ios::out respectively and can therefore be overridden. The mode for fstream must be specified.

The variable mode may include one or more of the following bits:

image from book

Open table as spreadsheet

Mode

Purpose

ios::app

used for appending to end of file

ios::ate

used to move to end of file after open

ios::binary

used to change from text to binary file

ios::in

used to permit input from file

ios::nocreate

used to open if the file already exists

ios::noreplace

used to open if the file does not exists

ios::out

used to permit output to the file

ios::trunc

used to remove all data from a file

image from book

Several of these bits can be joined by the bit operator: |

  • For example if you wanted to output to a binary file you would use: ios::out | ios::binary.

  • To permit text input and text output to the end of the file you would use: ios::in | ios::app.

  • To permit binary input and binary output at the end of the file: ios::in | ios::app | ios::binary.

For an example of using the mode: ios::app see: append.cpp. Run this program several times and then observe contents of the data file.

Now check the example: truncate.cpp. Notice that the mode: ios::ate is used. After you have run the program, look at the contents of the data file. Run it several times. Did the data file change? How?

Each of these I/O classes has several methods to include ones similar to the following for fstream:

image from book

 fstream()        // is a default constructor that does both                  // input and output but does not connect to                  // any specific data file fstream(const char *aFile, ios::mode aMode, int protect = S_IREAD|IWRITE)                           // non default constructor that connects to a                           // specific file: aFile and activates the mode: aMode ~fstream()       // destructor void open()      // opens the data file with respect to the fstream                  // object that is calling this method bool is_open()    // used to determine whether the fstream object                   // calling this method is open or not.                   // It returns true if the file is open                   // and false if the file did not open. bool eof()        // used to determine whether the fstream object                   // calling this method is at the end of the file.                   // It returns true if the reading is at the end of                   // the file and false otherwise. void close()      // closes the connection to the data file of the                   // fstream object that is calling this method 

image from book

When you attempt to open a file, you should test to see if it opened as in the following where aFile is an object of one of the classes in the header fstream:

image from book

 if(!aFile.is_open()) {     cerr << "The file can not open"     exit(1); } 

image from book

or

image from book

 if(!aFile) {     cerr << "The file can not open"     exit(1); } 

image from book

The last case works because the I/O object has value zero if the object did not connect to the I/O device and returns non zero if it did. The above discussion used the class fstream but these statements are also true for the stream classes: ifsteam and ofstream.

As you are reading data in from a file, you must take care that there is still data to be read. This could be done with a while() loop either using the name of the object like: aFile or by using the method: eof() as in the following examples:

image from book

 while(aFile) {          ...... } 

image from book

or

image from book

 while(!aFile.eof()) {       ......... } 

image from book

In the first example of testing for the end of the file, aFile will be non zero as long as there is data being read and it will be zero if data is not read. In the second example: aFile.eof() will be true if the end of the file has been reached and it will be false if the end of the file has not been reached.

Since each of these streams has a destructor, whenever an object like aFile goes out of scope, the file is closed. However it may be best to force the file to close as in:

image from book

 aFile.close(); 

image from book

Why would you want to use this member function? There are several reasons. One that will occur is that the file may be opened for one mode and it is desired to change to another mode. For example the first part of a program may be handling input and then the user decides to output the results to a printer or to a data file. Another reason is that some of the data may still be in the input or output buffer. By using this statement, the data will be forced out of the buffer.




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net