14.13. I/O Feature Summary

Review

More Portable Data Types

fmtflags Formatting flags: An unsigned 32-bit integer; each bit represents a flag that controls one aspect of the stream's behavior
openmode Represents the bit-vector values that specify file opening modes
streampos The position in a file. Represents the current read/write (get/put) position within a stream
streamsize The size of a file or a number of bytes. A signed integer
streamoff The offset within a file. Use by the "seek" functions
Portable data types used with strings. Unlike the data types whose names end with _t, and that are implemented as symbolic constants, the data types listed here are implemented with templates.

Bit-Masks / Formatting Flags

File Open Modes
ios::inOpen for input
ios::outOpen for output
ios::app Append data to the end of the output file (repositioning commands are ignored, forcing output at the end)
ios::ate Go to the end of the file when opened (allows repositioning in the file)
ios::truncDiscard contents of existing file
ios::binaryOpen in binary mode (defaults to text mode)
Formatting Flags
ios::decread/write integers as decimal (base-10) values
ios::hexread/write integers as hexadecimal (base-16) values
ios::octread/write integers as octal (base-8) values
ios::fixedforce printing of floating point numbers in fixed point notation
ios::scientificforce printing of floating point numbers in scientific notation
ios::leftalign on the left side, pad on the right
ios::rightalign on the right side, pad on the left
Positioning In A File
ios::begChange file pointer to the beginning of the file
ios::endChange file pointer to the end of the file
ios::curChange file pointer relative to the current position
Useful file I/O flags. See Open a file and fmtflags for additional formatting flags.

Manipulators

<iostream> Manipulators
Manipulator Description Example
endl Insert an end of line, \n, and flush the output buffer (the last character is a lower-case 'L') endl.cpp
ends Insert a null character, '\0', into the output stream  
dec Display output in decimal base.cpp
hex Display output in hexadecimal
oct Display output in octal
right Right-align, pad on the left (used with setw, see below). Used to format columns in a table - columns of numbers generally look best right justified. Output is right-justified by default; use "right" to reset justification if changed by "left" align.cpp
fill.cpp
left Left-align, pad on the right (used with setw, see below).
Used to format columns in a table - columns of strings generally look best left justified.
fixed Display numbers in a fixed decimal point notation (no scientific notation) notation.cpp
scientific Display numbers in scientific notation
<iomanip> Manipulators
setw(int width) Sets the output field width; if the output is shorter than the field with, the unused space is padded; the default pad character is a space but can be changed with "setfill." Used to format columns in a table. align.cpp
setfill(char fill) Sets the padding or fill character when the output field is wider the printed data. The default fill character is a space or blank. Used with setw. fill.cpp
setprecision(int precision) Sets the number of digits displayed after the decimal point (technically, the radix point) mortgage.cpp
setbase(int base) Sets the base of numeric output to decimal, hexadecimal, or octal
setiosflags(fmtflags flags) Sets the calling stream object's formatting flags to the argument value
resetiosflags(fmtflags flags) Sets to 0 all of the flags in the argument value
bool is_open() True if the file is open; false otherwise. Can be used with ifstream, ofstream, and fstream objects.
Useful Manipulators. See Format flag manipulators and <iomanip> for additional stream manipulators.

Constructors And Open Functions

Constructor Description Example
ifstream()
ifstream(file_name, ios::open mode = ios::in)
open(file_name, openmode mode = ios::in)
Creates a stream object that is not attached to a file.
Creates a stream object and opens the named file.
Opens a file with an existing ifstream object.
wc
HTMLfix
wc
ofstream()
ofstream(file_name, openmode mode = ios::out)
open(file_name, openmode mode = ios::out)
Creates a stream object that is not attached to a file.
Creates a stream object and opens the named file.
Opens a file with an existing ofstream object.
HTMLfix
fstream()
fstream(file_name, openmode mode = ios::in | ios::out)
open(file_name, openmode mode = ios::in | ios::out)
Creates a stream object that is not attached to a file.
Creates a stream object and opens the named file.
Opens a file with an existing fstream object.
rolodex(2)
File stream constructors. The "f" in each name indicates a file stream class. The "i" indicates an input stream, while the "o" indicates an output stream. file_name may be a C-string or an instance of the string class.

Member Functions

Function Description Example
ignore(n = 1, delim = '\n') Removes and discards n characters from the input stream, or stops after the delim character is removed or the end-of-file is reached, whichever comes first. For example: ignore(), ignore(10), or ignore(10, ':') temp.cpp
istream& read(char* s, streamsize n) Reads n bytes from the stream and saves them in the character buffer (array) s. mycopy.cpp
istream& seekg(streampos pos)
istream& seekg(streamoff off, [beg, cur, end])
Moves the "get" position pointer to the absolute position pos. Moves the "get" position pointer to the specified offset: beginning, current, or end (see Figure 2). rolodex(2)
streampos tellg() Returns the current position of the "get" position pointer.
streamsize gcount() Returns the number of byte input by the last read operation. mycopy.cpp
istream and ifstream member functions. See ifstream for more functions and details.
Function Description Example
char fill(char pad)
char fill();
Without an argument, returns the current fill character. With an argument, sets the padding or fill character when the output field is wider the printed data. The default fill character is a space or blank. Used with setw. Has the same effect as the setfill manipulator. fill2.cpp
precision(int digits) When either the fixed or the scientific formatting flags are set or are "on," the precision function sets the number of places following the decimal or radix point. See the example program for more details. precision.cpp
ostream& write(char* s, streamsize n) Writes n bytes to the stream from the character buffer (array) s. mycopy.cpp rolodex(2)
ostream& seekp(streampos pos)
ostream& seekg(streamoff off, [beg, cur, end])
Moves the "put" position pointer to the absolute position pos. Moves the "put" position pointer to the specified offset: beginning, current, or end (see Figure 2). rolodex(2)
streampos tellp() Returns the current position of the "put" position pointer. rolodex(2)
ostream& flush() Forces the system to write data stored in the stream buffer. rolodex(2)
ostream and ofstream member functions.

Flag Functions

Function Description Example
setf(flags) Sets those flags specified in flags while not changing the remaining flags. flags.cpp
mortgage.cpp
unsetf(flags) Clears those flags specified in flags while not changing the remaining flags.
flags() Gets the current formatting flags.
flags(flags) Sets those flags specified in flags while clearing the remaining flags.
Formatting flag functions.
Function Description Example
bool good() Returns true if there are not error flags set; returns false otherwise. mycopy.cpp
bool bad() Return true if the badbit is set.
bool fail() Return true if the failbit is set.
bool eof() Return true if the eofbit is set.
rdstat() Read state: returns a 4-bit vector containing the values of goodbit, badbit, failbit, and eofbit.
File state flag functions.