Methods of the C++ Stream Classes - dummies

Methods of the C++ Stream Classes

By Stephen R. Davis

The istream and ostream classes in C++ provide a number of methods, as shown in this table (this is not a complete list). The prototypes for these functions reside in the fstream include file.

bool bad()

Returns true if a serious error has occurred.

void clear(iostate flags = ios_base::goodbit)

Clears (or sets) the I/O state flags.

void close()

Closes the file associated with a stream object.

bool eof()

Returns true if no more characters are left in the file to be read.

iostate exception()

Returns the conditions that will cause an exception.

void exception(iostate)

Sets the conditions that will cause an exception. Multiple conditions can be ORed together; e.g., exception(ios_base::badbit|ios_base::failbit).

char fill()char fill(char newFill)

Returns or sets the fill character.

fmtflags flags()fmtflags flags(fmtflags f)

Returns or sets format flags.

void flush()

Flushes the output buffer to the disk.

int gcount()

Returns the number of bytes read during the last input.

char get()

Reads individual characters from the file.

char getline(  char* buffer,  int count,  char delimiter = 'n')

Reads multiple characters either until the end-of-file, until a delimiter is encountered, or until count – 1 characters read. Tack a null onto the end of the line read. Do not store the delimiter read into the buffer.

bool good()

Returns true if no error conditions are set.

void open( const char*   filename, openmode  mode = default)

Same arguments as the constructor. Performs the same file open on an existing object that the constructor performs when creating a new object.

streamsize precision()  streamsize precision(   streamsize s)

Reads or sets the number of digits displayed for floating-point variables.

ostream& put(char ch)

Writes a single character to the stream.

istream& read(  char* buffer,  streamsize num)

Reads a block of data. Reads either num bytes or until an end-of-file is encountered, whichever occurs first.

istream& seekg(  pos_type position)istream& seekg(   off_type offset,   ios_base::seekdir)

Positions the read pointer either position bytes from the beginning of the file or offset bytes from the current position.

istream& seekp(  pos_type position)istream& seekp(   off_type offset,   ios_base::seekdir)

Positions the write pointer.

fmtflags setf(fmtflags)

Sets specific format flags. Returns old value.

pos_type tellg()

Returns the position of the read pointer.

pos_type tellp()

Returns the position of the write pointer.

fmtflags unsetf(fmtflags)

Clears specific format flags. Returns old value.

int width()int width(int w)

Reads or sets the number of characters to be displayed by the next formatted output statement.

ostream& write(  const char* buffer,   streamsize num)

Writes a block of data to the output file.