Stream Functions In C++

In addition to providing the insertion and extraction operations, the stream classes define a number of other member functions that can be more convenient than using insertion and extra ction directly.
 
All these functions are discussed in some detail in the class descriptions later in this chapter. The following list briefly describes a few of the most useful member functions.
 
get() and getline()
 
allows extraction of characters from a stream until a delimiter (by default ‘ \n ‘) is encountered, possibly with a limit to the number of characters to be extracted.
 
The get() and getline() functions behave similarly, except that getline() extracts the final delimiter and get() does not.
 
read()
 
extracts a fixed number of characters from a stream. read() is intended for use with binary data, whereas get() and getline() are usually more appropriate with text data.
 
putback()
 
allows a character extracted from a stream to be “pushed back,” so that it will be extracted again the next time a character is required from the stream.
 
write()
 
inserts a number of characters into a stream. The null character is treated as any other character and therefore this function is suitable for inserting binary data.
 
flush()
 
immediately transmits any buffered characters. For a stream associated with a terminal file, this causes any buffered characters to be transmitted to the terminal.
 
For a nonterminal file, calling flush() may not cause any characters to be immediately written, depending on the characteristics of the file.
 
tie()
 
ties one stream to another stream, so that whenever the first file’s buffer is full or needs to be refilled, the tied file’s buffer is flushed.
 
The cin stream is automatically tied to cout, which means that cout ‘s buffer is flushed before characters are extracted from cin.
 
If, as is usually the case, cin and cout are both terminal files, this assures that you see any buffered output messages before having to enter a response.
 
Similarly, the cerr stream is tied to cout , so that if an error message is generated to cerr, any buffered output characters are written first.
 
seekg() and seekp()
 
are used to reposition a stream for input and output respectively.
 
tellg() and tellp()
 
The member functions tellg() and tellp() are used to determine the read or write position for a stream.
 
As with the seeking functions, the results of these functions are system-dependent.
 
Creating streams
 
Streams are normally created by declaring them or by use of the new operator. Creating an fstream or stdiostream entails opening the external file that is to be the source or sink of characters.
 
Creating a strstream entails specifying the area of storage that will serve as the source or sink of characters.
 
For fstream, a stream constructor can be used to create a stream associated with a particular file, similar to the way the fopen function is used in C.
 
For instance, the following declaration creates an output fstream object, dict, associated with the CMS file named DICT DATA:
 
ofstream dict(“cms:dict data”);
 
Opening files
 
When you create an fstream (or a stdiostream), you must usually provide a filename and an open mode. The open mode specifies the way in which the file is to be accessed.
 
For an ifstream, the default open mode is ios::in, specifying input only; for an ofstream, the default open mode is ios::out, specifying output only.
 
If you declare an fstream without specifying any arguments, a default constructor is called that creates an unopened fstream. An unopened stream can be opened by use of the member function open(), which accepts the same arguments as the constructor.
 
Defining a strstream
 
When you create a strstream, you must usually provide an area of memory and a length. Insertions to the stream store into the area of memory; extractions return successive characters from the area.
 
When the array is full, no more characters can be inserted; when all characters have been extracted, the ios::eof flag is set for the stream.
 
For an istrstream, the length argument to the constructor is optional; if you omit it, the end of the storage area is determined by scanning for an end-of-string delimiter (‘ \0 ‘).
 
For a strstream that permits output, you can create a dynamic stream by using a constructor with no arguments. In this case, memory is allocated dynamically to hold inserted characters.
 
When all characters have been inserted, you can use the member function str() to “freeze” the stream. This prevents further insertions into the stream and returns the address of the area where previously inserted characters have been stored.