Online Book Reader

Home Category

Professional C__ - Marc Gregoire [275]

By Root 1515 0
this would

// not flush because std::endl was not sent.

outFile << "Hello there!";

// outFile has NOT been flushed.

// Read some text from inFile. This will trigger flush()

// on outFile.

string nextToken;

inFile >> nextToken;

// outFile HAS been flushed.

Code snippet from tie\tie.cpp

The flush() method is defined on the ostream base class, so you can also link an output stream to another output stream:

outFile.tie(&anotherOutputFile);

Such a relationship would mean that every time you wrote to one file, the buffered data that had been sent to the other file would be written. You could use this mechanism to keep two related files synchronized.

One example of this stream linking is the link between cout and cin. Whenever you try to input data from cin, cout is automatically flushed.

BIDIRECTIONAL I/O


So far, this chapter has discussed input and output streams as two separate but related classes. In fact, there is such a thing as a stream that performs both input and output. A bidirectional stream operates as both an input stream and an output stream.

Bidirectional streams are subclasses of iostream, which in turn subclasses both istream and ostream, thus serving as an example of useful multiple inheritance. As you would expect, bidirectional streams support both the >> operator and the << operator, as well as the methods of both input streams and output streams.

The fstream class provides a bidirectional file stream. fstream is ideal for applications that need to replace data within a file because you can read until you find the correct position, then immediately switch to writing. For example, imagine a program that stores a list of mappings between ID numbers and phone numbers. It might use a data file with the following format:

123 408-555-0394

124 415-555-3422

164 585-555-3490

100 650-555-3434

A reasonable approach to such a program would be to read in the entire data file when the program opens and rewrite the file, with any modifications, when the program closes. If the data set is huge, however, you might not be able to keep everything in memory. With iostreams, you don’t have to. You can easily scan through the file to find a record, and you can add new records by opening the file for output in append mode. To modify an existing record, you could use a bidirectional stream, as in the following function that changes the phone number for a given ID:

bool changeNumberForID(const string& inFileName, int inID,

const string& inNewNumber)

{

fstream ioData(inFileName.c_str());

if (!ioData) {

cerr << "Error while opening file " << inFileName << endl;

return false;

}

// Loop until the end of file

while (ioData.good()) {

int id;

string number;

// Read the next ID.

ioData >> id;

// Check to see if the current record is the one being changed.

if (id == inID) {

// Seek to the current read position

ioData.seekp(ioData.tellg());

// Output a space, then the new number.

ioData << " " << inNewNumber;

break;

}

// Read the current number to advance the stream.

ioData >> number;

}

return true;

}

Code snippet from Bidirectional\Bidirectional.cpp

Of course, an approach like this will work properly only if the data is of a fixed size. When the preceding program switched from reading to writing, the output data overwrote other data in the file. To preserve the format of the file, and to avoid writing over the next record, the data had to be the exact same size.

String streams can also be accessed in a bidirectional manner through the stringstream class.

Bidirectional streams have separate pointers for the read position and the write position. When switching between reading and writing, you will need to seek to the appropriate position.

SUMMARY


Streams provide a flexible and object-oriented way to perform input and output. The most important message in this chapter, even more important than the use of streams, is the concept of a stream. Some operating systems may have their own file access and I/O facilities, but knowledge of how streams and stream-like libraries work is

Return Main Page Previous Page Next Page

®Online Book Reader