Online Book Reader

Home Category

Professional C__ - Marc Gregoire [273]

By Root 1382 0
For example, if you need to append a lot of strings together, it might be more efficient to use a string stream, instead of repeatedly calling the += operator on a string object.

FILE STREAMS


Files lend themselves very well to the stream abstraction because reading and writing files always involves a position in addition to the data. In C++, the ofstream and ifstream classes provide output and input functionality for files. They are defined in the header file.

When dealing with the file system, it is especially important to detect and handle error cases. The file you are working with could be on a network file store that just went offline, or you may be trying to write to a file that is located on a disk that is full. Maybe you are trying to open a file to which the current user does not have permissions to. Error conditions can be detected by using the standard error handling mechanisms described earlier.

The only major difference between output file streams and other output streams is that the file stream constructor can take the name of the file and the mode in which you would like to open it. The default mode is write, ios_base::out, which starts writing to a file at the beginning, overwriting any existing data. You can also open an output file stream in append mode by specifying the constant ios_base::app as second argument to the file stream constructor. The following table lists the different constants that are available:

CONSTANT DESCRIPTION

ios_base::app Open and go to the end before each write operation.

ios_base::ate Open and go to the end immediately after opening.

ios_base::binary Perform input and output in binary mode (as opposed to text mode).

ios_base::in Open for input.

ios_base::out Open for output.

ios_base::trunc Open and truncate any existing data.

The following program opens the file test and outputs the arguments to the program. The ifstream and ofstream destructors will automatically close the underlying file, so there is no need to explicitly call close():

int main(int argc, char* argv[])

{

ofstream outFile("test");

if (!outFile.good()) {

cerr << "Error while opening output file!" << endl;

return -1;

}

outFile << "There were " << argc << " arguments to this program." << endl;

outFile << "They are: " << endl;

for (int i = 0; i < argc; i++) {

outFile << argv[i] << endl;

}

return 0;

}

Code snippet from FileStream\FileStream1.cpp

Jumping around with seek() and tell()

The seek() and tell() methods are present on all input and output streams, but they rarely make sense outside of the context of file streams.

The seek() methods let you move to an arbitrary position within an input or output stream. There are several forms of seek(). The methods of seek() within an input stream are actually called seekg() (the g is for get), and the versions of seek() in an output stream are called seekp() (the p is for put). You might wonder why there is both a seekg() and a seekp() method, instead of one seek() method. The reason is that you can have streams that are both input and output, for example, file streams. In that case, the stream needs to remember both a read position and a separate write position. This is also called bidirectional I/O and is covered later in this chapter.

There are two overloads of seekg() and two of seekp(). One overload accepts a single argument, an absolute position, and seeks to this absolute position. The second overload accepts an offset and a position, and seeks an offset relative to the given position. Positions are of type ios_base::streampos, while offsets are of type ios_base::streamoff, both are measured in bytes. There are three predefined positions available:

POSITION DESCRIPTION

ios_base::beg The beginning of the stream

ios_base::end The end of the stream

ios_base::cur The current position in the stream

For example, to seek to an absolute position in an output stream, you can use the one-parameter version of seekp(), as in the following case, which uses the constant ios_base::beg to move to the beginning of the stream:

outStream.seekp(ios_base::beg);

Return Main Page Previous Page Next Page

®Online Book Reader