| C++ Library Reference |
The Classic
iostreamLibraryC++, like C, has no built-in input or output statements. Instead, I/O facilities are provided by a library. The Sun WorkShop 6 C++ compiler provides both the classic implementation and the ISO standard implementation of the
iostreamclasses.
- In compatibility mode (
-compat[=4]), the classiciostreamclasses are contained inlibC.- In standard mode (default mode), the classic
iostreamclasses are contained inlibiostream. Uselibiostreamwhen you have source code that uses the classiciostreamclasses and you want to compile the source in standard mode. To use the classiciostreamfacilities in standard mode, include theiostream.hheader file and compile using the-library=iostreamoption.- The standard
iostreamclasses are available only in standard mode, and are contained in the C++ standard library,libCstd.This chapter provides an introduction to the classic
iostreamlibrary and provides examples of its use. This chapter does not provide a complete description of theiostreamlibrary. See theiostreamlibrary man pages for more details. To access the classiciostreamman pages type:
example%man -s 3CC4name3.1 Predefined
iostreamsThere are four predefined
iostreams:
cin, connected to standard inputcout, connected to standard outputcerr, connected to standard errorclog, connected to standard errorThe predefined
iostreamsare fully buffered, except forcerr. See Section 3.3.1 "Output Using iostream" and Section 3.3.2 "Input Using iostream".3.2 Basic Structure of
iostreamInteractionBy including the
iostreamlibrary, a program can use any number of input or output streams. Each stream has some source or sink, which may be one of the following:
- Standard input
- Standard output
- Standard error
- A file
- An array of characters
A stream can be restricted to input or output, or a single stream can allow both input and output. The
iostreamlibrary implements these streams using two processing layers.
- The lower layer implements sequences, which are simply streams of characters. These sequences are implemented by the
streambufclass, or by classes derived from it.- The upper layer performs formatting operations on sequences. These formatting operations are implemented by the
istreamandostreamclasses, which have as a member an object of a type derived from classstreambuf. An additional class,iostream, is for streams on which both input and output can be performed.Standard input, output, and error are handled by special class objects derived from class
istreamorostream.The
ifstream,ofstream, andfstreamclasses, which are derived fromistream,ostream, andiostreamrespectively, handle input and output with files.The
istrstream,ostrstream, andstrstreamclasses, which are derived fromistream,ostream, andiostreamrespectively, handle input and output to and from arrays of characters.When you open an input or output stream, you create an object of one of these types, and associate the
streambufmember of the stream with a device or file. You generally do this association through the stream constructor, so you don't work with thestreambufdirectly. Theiostreamlibrary predefines stream objects for the standard input, standard output, and error output, so you don't have to create your own objects for those streams.You use operators or
iostreammember functions to insert data into a stream (output) or extract data from a stream (input), and to control the format of data that you insert or extract.When you want to insert and extract a new data type--one of your classes--you generally overload the insertion and extraction operators.
3.3 Using the Classic
iostreamLibraryTo use routines from the classic
iostreamlibrary, you must include the header files for the part of the library you need. The header files are described in the following table.
You usually do not need all of these header files in your program. Include only the ones that contain the declarations you need. In compatibility mode (
-compat[=4]), the classiciostreamlibrary is part oflibC, and is linked automatically by theCCdriver. In standard mode (the default),libiostreamcontains the classiciostreamlibrary.3.3.1 Output Using
iostreamOutput using
iostreamusually relies on the overloaded left-shift operator (<<) which, in the context ofiostream, is called the insertion operator. To output a value to standard output, you insert the value in the predefined output streamcout. For example, given a valuesomeValue, you send it to standard output with a statement like:
cout << someValue;The insertion operator is overloaded for all built-in types, and the value represented by
someValueis converted to its proper output representation. If, for example,someValueis afloatvalue, the<<operator converts the value to the proper sequence of digits with a decimal point. Where it insertsfloatvalues on the output stream,<<is called the float inserter. In general, given a typeX,<<is called theXinserter. The format of output and how you can control it is discussed in theios(3CC4) man page.The
iostreamlibrary does not support user-defined types. If you define types that you want to output in your own way, you must define an inserter (that is, overload the<<operator) to handle them correctly.The
<<operator can be applied repetitively. To insert two values oncout, you can use a statement like the one in the following example:
cout << someValue << anotherValue;The output from the above example will show no space between the two values. So you may want to write the code this way:
cout << someValue << " " << anotherValue;The << operator has the precedence of the left shift operator (its built-in meaning). As with other operators, you can always use parentheses to specify the order of action. It is often a good idea to use parentheses to avoid problems of precedence. Of the following four statements, the first two are equivalent, but the last two are not.
cout << a+b; // + has higher precedence than <<cout << (a+b);cout << (a&y); // << has precedence higher than &cout << a&y; // probably an error: (cout << a) & y3.3.1.1 Defining Your Own Insertion Operator
The following example defines a
stringclass:
The insertion and extraction operators must in this case be defined as friends because the data part of the
stringclass isprivate.
ostream& operator<< (ostream& ostr, const string& output){ return ostr << output.data; }Here is the definition of
operator<<overloaded for use withstrings.
cout << string1 << string2;
operator<<takesostream&(that is, a reference to anostream) as its first argument and returns the sameostream, making it possible to combine insertions in one statement.3.3.1.2 Handling Output Errors
Generally, you don't have to check for errors when you overload
operator<<because theiostreamlibrary is arranged to propagate errors.When an error occurs, the
iostreamwhere it occurred enters an error state. Bits in theiostream's state are set according to the general category of the error. The inserters defined iniostreamignore attempts to insert data into any stream that is in an error state, so such attempts do not change theiostream's state.In general, the recommended way to handle errors is to periodically check the state of the output stream in some central place. If there is an error, you should handle it in some way. This chapter assumes that you define a function
error, which takes a string and aborts the program.erroris not a predefined function. See Section 3.3.9 "Handling Input Errors" for an example of anerrorfunction. You can examine the state of aniostreamwith the operator!,which returns a nonzero value if theiostreamis in an error state. For example:
if (!cout) error( "output error");There is another way to test for errors. The
iosclass definesoperatorvoid*(),so it returns a NULL pointer when there is an error. You can use a statement like:
if (cout << x) return ; // return if successfulYou can also use the function
good, a member ofios:
if ( cout.good() ) return ; // return if successfulThe error bits are declared in the
enum:
enum io_state { goodbit=0, eofbit=1, failbit=2,badbit=4, hardfail=0x80} ;For details on the error functions, see the
iostreamman pages.3.3.1.3 Flushing
As with most I/O libraries,
iostreamoften accumulates output and sends it on in larger and generally more efficient chunks. If you want to flush the buffer, you simply insert the special valueflush. For example:
cout << "This needs to get out immediately." << flush ;
flushis an example of a kind of object known as a manipulator, which is a value that can be inserted into aniostreamto have some effect other than causing output of its value. It is really a function that takes anostream&oristream&argument and returns its argument after performing some actions on it (see Section 3.7 "Manipulators").3.3.1.4 Binary Output
To obtain output in the raw binary form of a value, use the member function
writeas shown in the following example. This example shows the output in the raw binary form ofx.
cout.write((char*)&x, sizeof(x));The previous example violates type discipline by converting
&xtochar*. Doing so is normally harmless, but if the type ofxis a class with pointers, virtual member functions, or one that requires nontrivial constructor actions, the value written by the above example cannot be read back in properly.3.3.2 Input Using
iostreamInput using
iostreamis similar to output. You use the extraction operator>>and you can string together extractions the way you can with insertions. For example:
cin >> a >> b ;This statement gets two values from standard input. As with other overloaded operators, the extractors used depend on the types of
aandb(and two different extractors are used ifaandbhave different types). The format of input and how you can control it is discussed in some detail in theios(3CC4) man page. In general, leading whitespace characters (spaces, newlines, tabs, form-feeds, and so on) are ignored.3.3.3 Defining Your Own Extraction Operators
When you want input for a new type, you overload the extraction operator for it, just as you overload the insertion operator for output.
Class
stringdefines its extraction operator in the following code example:
The
getfunction reads characters from the input streamistrand stores them inholderuntilmaxline-1characters have been read, or a new line is encountered, or EOF, whichever happens first. The data inholderis then null-terminated. Finally, the characters inholderare copied into the target string.By convention, an extractor converts characters from its first argument (in this case,
istream&istr), stores them in its second argument, which is always a reference, and returns its first argument. The second argument must be a reference because an extractor is meant to store the input value in its second argument.3.3.4 Using the
char*ExtractorThis predefined extractor is mentioned here because it can cause problems. Use it like this:
char x[50];cin >> x;This extractor skips leading whitespace and extracts characters and copies them to
xuntil it reaches another whitespace character. It then completes the string with a terminating null (0) character. Be careful, because input can overflow the given array.You must also be sure the pointer points to allocated storage. For example, here is a common error:
char * p; // not initializedcin >> p;There is no telling where the input data will be stored, and it may cause your program to abort.
3.3.5 Reading Any Single Character
In addition to using the
charextractor, you can get a single character with either form of thegetmember function. For example:
char c;cin.get(c); // leaves c unchanged if input failsint b;b = cin.get(); // sets b to EOF if input fails
Note – Unlike the other extractors, thecharextractor does not skip leading whitespace.
Here is a way to skip only blanks, stopping on a tab, newline, or any other character:
int a;do {a = cin.get();}while( a == ' ' );3.3.6 Binary Input
If you need to read binary values (such as those written with the member function
write), you can use thereadmember function. The following example shows how to input the raw binary form ofxusing thereadmember function, and is the inverse of the earlier example that useswrite.
cin.read((char*)&x, sizeof(x));3.3.7 Peeking at Input
You can use the
peekmember function to look at the next character in the stream without extracting it. For example:
if (cin.peek() != c) return 0;3.3.8 Extracting Whitespace
By default, the
iostreamextractors skip leading whitespace. You can turn off the skip flag to prevent this from happening. The following example turns off whitespace skipping fromcin, then turns it back on:
cin.unsetf(ios::skipws); // turn off whitespace skipping. . .cin.setf(ios::skipws); // turn it on againYou can use the
iostreammanipulatorwsto remove leading whitespace from theiostream, whether or not skipping is enabled. The following example shows how to remove the leading whitespace fromiostreamistr:
istr >> ws;3.3.9 Handling Input Errors
By convention, an extractor whose first argument has a nonzero error state should not extract anything from the input stream and should not clear any error bits. An extractor that fails should set at least one error bit.
As with output errors, you should check the error state periodically and take some action, such as aborting, when you find a nonzero state. The
!operator tests the error state of aniostream. For example, the following code produces an input error if you type alphabetic characters for input:
#include <unistd.h>#include <iostream.h>void error (const char* message) {cerr << message << "\n" ;exit(1);}int main() {cout << "Enter some characters: ";int bad;cin >> bad;if (!cin) error("aborted due to input error");cout << "If you see this, not an error." << "\n";return 0;}Class
ioshas member functions that you can use for error handling. See the man pages for details.3.3.10 Using
iostreamswithstdioYou can use
stdiowith C++ programs, but problems can occur when you mixiostreamsandstdioin the same standard stream within a program. For example, if you write to bothstdoutandcout, independent buffering occurs and produces unexpected results. The problem is worse if you input from bothstdinandcin, since independent buffering may turn the input into trash.To eliminate this problem with standard input, standard output and standard error, use the following instruction before performing any input or output. It connects all the predefined
iostreamswith the corresponding predefinedstdioFILEs.
ios::sync_with_stdio();Such a connection is not the default because there is a significant performance penalty when the predefined streams are made unbuffered as part of the connection. You can use both
stdioandiostreamsin the same program applied to different files. That is, you can write tostdoutusingstdioroutines and write to other files attached toiostreams. You can openstdio FILEs for input and also read fromcinso long as you don't also try to read fromstdin.3.4 Creating
iostreamsTo read or write a stream other than the predefined
iostreams, you need to create your owniostream. In general, that means creating objects of types defined in theiostreamlibrary. This section discusses the various types available.3.4.1 Dealing with Files Using Class
fstreamDealing with files is similar to dealing with standard input and standard output; classes
ifstream,ofstream, andfstreamare derived from classesistream,ostream, andiostream, respectively. As derived classes, they inherit the insertion and extraction operations (along with the other member functions) and also have members and constructors for use with files.Include the file
fstream.hto use any of thefstreams. Use anifstreamwhen you only want to perform input, anofstreamfor output only, and anfstreamfor a stream on which you want to perform both input and output. Use the name of the file as the constructor argument.For example, copy the file
thisFileto the filethatFileas in the following example:
ifstream fromFile("thisFile");if (!fromFile)error("unable to open 'thisFile' for input");ofstream toFile ("thatFile");if ( !toFile )error("unable to open 'thatFile' for output");char c ;while (toFile && fromFile.get(c)) toFile.put(c);
- Creates an
ifstreamobject calledfromFilewith a default mode ofios::inand connects it tothisFile. It opensthisFile.- Checks the error state of the new
ifstreamobject and, if it is in a failed state, calls theerrorfunction, which must be defined elsewhere in the program.- Creates an
ofstreamobject calledtoFilewith a default mode ofios::outand connects it tothatFile.- Checks the error state of
toFileas above.- Creates a
charvariable to hold the data while it is passed.- Copies the contents of
fromFiletotoFileone character at a time.
Note – It is, of course, undesirable to copy a file this way, one character at a time. This code is provided just as an example of usingfstreams. You should instead insert thestreambufassociated with the input stream into the output stream. See Section 3.10 "Streambufs", and the man pagesbufpub(3CC4).
3.4.1.1 Open Mode
The mode is constructed by
or-ing bits from the enumerated typeopen_mode, which is a public type of classiosand has the definition:
enum open_mode {binary=0, in=1, out=2, ate=4, app=8, trunc=0x10,nocreate=0x20, noreplace=0x40};
Note – Thebinaryflag is not needed on Unix, but is provided for compatibility with systems that do need it. Portable code should use thebinaryflag when opening binary files.
You can open a file for both input and output. For example, the following code opens file
someNamefor both input and output, attaching it to thefstreamvariableinoutFile.
fstream inoutFile("someName", ios::in|ios::out);3.4.1.2 Declaring an
fstreamWithout Specifying a FileYou can declare an
fstreamwithout specifying a file and open the file later. For example, the following creates theofstreamtoFilefor writing.
ofstream toFile;toFile.open(argv[1], ios::out);3.4.1.3 Opening and Closing Files
You can close the
fstreamand then open it with another file. For example, to process a list of files provided on the command line:
ifstream infile;for (char** f = &argv[1]; *f; ++f) {infile.open(*f, ios::in);...;infile.close();}3.4.1.4 Opening a File Using a File Descriptor
If you know a file descriptor, such as the integer
1for standard output, you can open it like this:
ofstream outfile;outfile.attach(1);When you open a file by providing its name to one of the
fstreamconstructors or by using theopenfunction, the file is automatically closed when thefstreamis destroyed (by adeleteor when it goes out of scope). When youattacha file to anfstream, it is not automatically closed.3.4.1.5 Repositioning within a File
You can alter the reading and writing position in a file. Several tools are supplied for this purpose.
streamposis a type that can record a position in aniostream.tellg(tellp)is anistream(ostream)member function that reports the file position. Sinceistreamandostreamare the parent classes offstream,tellgandtellpcan also be invoked as a member function of thefstreamclass.seekg(seekp)is anistream(ostream)member function that finds a given position.- The
seek_direnumspecifies relative positions for use withseek.
enum seek_dir { beg=0, cur=1, end=2 }For example, given an
fstreamaFile:
streampos original = aFile.tellp(); //save current positionaFile.seekp(0, ios::end); //reposition to end of fileaFile << x; //write a value to fileaFile.seekp(original); //return to original position
seekg(seekp) can take one or two parameters. When it has two parameters, the first is a position relative to the position indicated by theseek_dirvalue given as the second parameter. For example:
aFile.seekp(-10, ios::end);moves to 10 bytes from the end while
aFile.seekp(10, ios::cur);moves to 10 bytes forward from the current position.
Note – Arbitrary seeks on text streams are not portable, but you can always return to a previously savedstreamposvalue.
3.5 Assignment of
iostreams
iostreamsdoes not allow assignment of one stream to another.The problem with copying a stream object is that there are then two versions of the state information, such as a pointer to the current write position within an output file, which can be changed independently. As a result, problems could occur.
3.6 Format Control
Format control is discussed in detail in the in the man page
ios(3CC4).3.7 Manipulators
Manipulators are values that you can insert into or extract from
iostreamsto have special effects.Parameterized manipulators are manipulators that take one or more parameters.
Because manipulators are ordinary identifiers, and therefore use up possible names,
iostreamdoesn't define them for every possible function. A number of manipulators are discussed with member functions in other parts of this chapter.There are 13 predefined manipulators, as described in TABLE 3-2. When using that table, assume the following:
ihas typelong.nhas typeint.chas typechar.istris an input stream.ostris an output stream.
TABLE 3-2 iostreamPredefined ManipulatorsPredefined Manipulator Description 1 ostr << dec, istr >> decMakes the integer conversion base 10. 2 ostr << endlInserts a newline character ( '\n') and invokesostream::flush().3 ostr << endsInserts a null (0) character. Useful when dealing with strstreams.4 ostr << flushInvokes ostream::flush().5 ostr << hex, istr >> hexMakes the integer conversion base 16. 6 ostr << oct, istr >> octMake the integer conversion base 8. 7 istr >> wsExtracts whitespace characters (skips whitespace) until a non-whitespace character is found (which is left in istr).8 ostr << setbase(n), istr >> setbase(n)Sets the conversion base to n(0, 8, 10, 16 only).9 ostr << setw(n), istr >> setw(n)Invokes ios::width(n). Sets the field width to n.10 ostr << resetiosflags(i), istr >> resetiosflags(i)Clears the flags bitvector according to the bits set in i.11 ostr << setiosflags(i), istr >> setiosflags(i)Sets the flags bitvector according to the bits set in i.12 ostr << setfill(c), istr >> setfill(c)Sets the fill character (for padding a field) to c.13 ostr << setprecision(n), istr >> setprecision(n)Sets the floating-point precision to ndigits.To use predefined manipulators, you must include the file
iomanip.hin your program.You can define your own manipulators. There are two basic types of manipulator:
- Plain manipulator--Takes an
istream&,ostream&, orios&argument, operates on the stream, and then returns its argument.- Parameterized manipulator--Takes an
istream&,ostream&, orios&argument, one additional argument (the parameter), operates on the stream, and then returns its stream argument.3.7.1 Using Plain Manipulators
A plain manipulator is a function that:
- Takes a reference to a stream
- Operates on it in some way
- Returns its argument
The shift operators taking (a pointer to) such a function are predefined for
iostreams, so the function can be put in a sequence of input or output operators. The shift operator calls the function rather than trying to read or write a value. An example of atabmanipulator that inserts atabin anostreamis:
ostream& tab(ostream& os) {return os << '\t' ;}...cout << x << tab << y ;This is an elaborate way to achieve the following:
const char tab = '\t';...cout << x << tab << y;Here is another example, which cannot be accomplished with a simple constant. Suppose you want to turn whitespace skipping on and off for an input stream. You can use separate calls to
ios::setfandios::unsetfto turn theskipwsflag on and off, or you could define two manipulators:
#include <iostream.h>#include <iomanip.h>istream& skipon(istream &is) {is.setf(ios::skipws, ios::skipws);return is;}istream& skipoff(istream& is) {is.unsetf(ios::skipws);return is;}...int main (){int x,y;cin >> skipon >> x >> skipoff >> y;return 1;}3.7.2 Parameterized Manipulators
One of the parameterized manipulators that is included in
iomanip.hissetfill.setfillsets the character that is used to fill out field widths. It is implemented as shown in the following example:
//file setfill.cc#include<iostream.h>#include<iomanip.h>//the private manipulatorstatic ios& sfill(ios& i, int f) {i.fill(f);return i;}//the public applicatorsmanip_int setfill(int f) {return smanip_int(sfill, f);}A parameterized manipulator is implemented in two parts:
- The manipulator. It takes an extra parameter. In the previous code example, it takes an extra
intparameter. You cannot place this manipulator function in a sequence of input or output operations, since there is no shift operator defined for it. Instead, you must use an auxiliary function, the applicator.- The applicator. It calls the manipulator. The applicator is a global function, and you make a prototype for it available in a header file. Usually the manipulator is a static function in the file containing the source code for the applicator. The manipulator is called only by the applicator, and if you make it static, you keep its name out of the global address space.
Several classes are defined in the header file
iomanip.h. Each class holds the address of a manipulator function and the value of one parameter. Theiomanipclasses are described in the man pagemanip(3CC4). The previous example uses thesmanip_intclass, which works with anios. Because it works with anios, it also works with anistreamand anostream. The previous example also uses a second parameter of typeint.The applicator creates and returns a class object. In the previous code example the class object is an
smanip_int, and it contains the manipulator and theintargument to the applicator. Theiomanip.hheader file defines the shift operators for this class. When the applicator functionsetfillappears in a sequence of input or output operations, the applicator function is called, and it returns a class. The shift operator acts on the class to call the manipulator function with its parameter value, which is stored in the class.In the following example, the manipulator
print_hex:
- Puts the output stream into the hex mode.
- Inserts a
longvalue into the stream.- Restores the conversion mode of the stream.
The class
omanip_longis used because this code example is for output only, and it operates on alongrather than anint:
#include <iostream.h>#include <iomanip.h>static ostream& xfield(ostream& os, long v) {long save = os.setf(ios::hex, ios::basefield);os << v;os.setf(save, ios::basefield);return os;}omanip_long print_hex(long v) {return omanip_long(xfield, v);}3.8
Strstreams:iostreamsfor ArraysSee the
strstream(3CC4) man page.3.9
Stdiobufs:iostreamsforstdioFilesSee the
stdiobuf(3CC4) man page.3.10
Streambufs
iostreamsare the formatting part of a two-part (input or output) system. The other part of the system is made up ofstreambufs, which deal in input or output of unformatted streams of characters.You usually use
streambufs throughiostreams, so you don't have to worry about the details ofstreambufs. You can usestreambufs directly if you choose to, for example, if you need to improve efficiency or to get around the error handling or formatting built intoiostreams.3.10.1 Working with
StreambufsA
streambufconsists of a stream or sequence of characters and one or two pointers into that sequence. Each pointer points between two characters. (Pointers cannot actually point between characters, but it is helpful to think of them that way.) There are two kinds ofstreambufpointers:
- A put pointer, which points just before the position where the next character will be stored
- A get pointer, which points just before the next character to be fetched
A
streambufcan have one or both of these pointers.3.10.1.1 Position of Pointers
The positions of the pointers and the contents of the sequences can be manipulated in various ways. Whether or not both pointers move when manipulated depends on the kind of
streambufused. Generally, with queue-likestreambufs, the get and put pointers move independently; with file-likestreambufs the get and put pointers always move together. Astrstreamis an example of a queue-like stream; anfstreamis an example of a file-like stream.3.10.2 Using
StreambufsYou never create an actual
streambufobject, but only objects of classes derived from classstreambuf. Examples arefilebufandstrstreambuf, which are described in man pagesfilebuf(3CC4) andssbuf(3), respectively. Advanced users may want to derive their own classes fromstreambufto provide an interface to a special device or to provide other than basic buffering. Man pagessbufpub(3CC4) andsbufprot(3CC4) discuss how to do this.Apart from creating your own special kind of
streambuf, you may want to access thestreambufassociated with aniostreamto access the public member functions, as described in the man pages referenced above. In addition, eachiostreamhas a defined inserter and extractor which takes astreambufpointer. When astreambufis inserted or extracted, the entire stream is copied.Here is another way to do the file copy discussed earlier, with the error checking omitted for clarity:
ifstream fromFile("thisFile");ofstream toFile ("thatFile");toFile << fromFile.rdbuf();We open the input and output files as before. Every
iostreamclass has a member functionrdbufthat returns a pointer to thestreambufobject associated with it. In the case of anfstream, thestreambufobject is typefilebuf. The entire file associated withfromFileis copied (inserted into) the file associated withtoFile. The last line could also be written like this:
fromFile >> toFile.rdbuf();The source file is then extracted into the destination. The two methods are entirely equivalent.
3.11
iostreamMan PagesA number of C++ man pages give details of the
iostreamlibrary. The following table gives an overview of what is in each man page.To access a classic
iostreamlibrary man page, type:
example%man -s 3CC4name
TABLE 3-3 iostreamMan Pages OverviewMan Page Overview filebufDetails the public interface for the class filebuf, which is derived fromstreambufand is specialized for use with files. See thesbufpub(3CC4) andsbufprot(3CC4) man pages for details of features inherited from classstreambuf. Use thefilebufclass through classfstream.fstreamDetails specialized member functions of classes ifstream,ofstream, andfstream, which are specialized versions ofistream,ostream, andiostreamfor use with files.iosDetails parts of class ios, which functions as a base class foriostreams. It contains state data common to all streams.ios.introGives an introduction to and overview of iostreams.istreamDetails the following:
Member functions for class istream, which supports interpretation of characters fetched from astreambufInput formatting Positioning functions described as part of class ostream.Some related functions Related manipulators manipDescribes the input and output manipulators defined in the iostreamlibrary.ostreamDetails the following:
Member functions for class ostream, which supports interpretation of characters written to astreambufOutput formatting Positioning functions described as part of class ostreamSome related functions Related manipulators sbufprotDescribes the interface needed by programmers who are coding a class derived from class streambuf. Also refer to thesbufpub(3CC4) man page because some public functions are not discussed in thesbufprot(3CC4) man page.sbufpubDetails the public interface of class streambuf, in particular, the public member functions ofstreambuf. This man page contains the information needed to manipulate astreambuf-type object directly, or to find out about functions that classes derived fromstreambufinherit from it. If you want to derive a class fromstreambuf, also see thesbufprot(3CC4) man page.ssbufDetails the specialized public interface of class strstreambuf, which is derived fromstreambufand specialized for dealing with arrays of characters. See thesbufpub(3CC4) man page for details of features inherited from classstreambuf.stdiobufContains a minimal description of class stdiobuf, which is derived fromstreambufand specialized for dealing withstdioFILEs. See thesbufpub(3CC4) man page for details of features inherited from classstreambuf.strstreamDetails the specialized member functions of strstreams, which are implemented by a set of classes derived from theiostreamclasses and specialized for dealing with arrays of characters.
3.12
iostreamTerminologyThe
iostreamlibrary descriptions often use terms similar to terms from general programming, but with specialized meanings. The following table defines these terms as they are used in discussing theiostreamlibrary.
TABLE 3-4 iostreamTerminologyiostreamTermDefinition Buffer A word with two meanings, one specific to the iostreampackage and one more generally applied to input and output.
When referring specifically to theiostreamlibrary, a buffer is an object of the type defined by the classstreambuf.
A buffer, generally, is a block of memory used to make efficient transfer of characters for input of output. With buffered I/O, the actual transfer of characters is delayed until the buffer is full or forcibly flushed. An unbuffered buffer refers to astreambufwhere there is no buffer in the general sense defined above. This chapter avoids use of the term buffer to refer tostreambufs. However, the man pages and other C++ documentation do use the term buffer to meanstreambufs.Extraction The process of taking input from an iostream.FstreamAn input or output stream specialized for use with files. Refers specifically to a class derived from class iostreamwhen printed incourierfont.Insertion The process of sending output into an iostream.iostreamGenerally, an input or output stream. iostreamlibraryThe library implemented by the include files iostream.h,fstream.h,strstream.h,iomanip.h, andstdiostream.h. Becauseiostreamis an object-oriented library, you should extend it. So, some of what you can do with theiostreamlibrary is not implemented.Stream An iostream,fstream,strstream, or user-defined stream in general.StreambufA buffer that contains a sequence of characters with a put or get pointer, or both. When printed in courierfont, it means the particular class. Otherwise, it refers generally to any object of classstreambufor a class derived fromstreambuf. Any stream object contains an object, or a pointer to an object, of a type derived fromstreambuf.StrstreamAn iostreamspecialized for use with character arrays. It refers to the specific class when printed incourierfont.
| Sun
Microsystems, Inc. Copyright information. All rights reserved. Feedback |
Library | Contents | Previous | Next | Index |