Go to the first, previous, next, last section, table of contents.

Reading strings

Use these methods to read strings (for example, a line at a time) from the input stream:

Method: istream& istream::get (char* c, int len [, char delim])

Read a string from the input stream, into the array at c.

The remaining arguments limit how much to read: up to `len-1' characters, or up to (but not including) the first occurrence in the input of a particular delimiter character delim---newline (\n) by default. (Naturally, if the stream reaches end of file first, that too will terminate reading.)

If delim was present in the input, it remains available as if unread; to discard it instead, see iostream::getline.

get writes `\0' at the end of the string, regardless of which condition terminates the read.

Method: istream& istream::get (streambuf& sb [, char delim])

Read characters from the input stream and copy them on the streambuf object sb. Copying ends either just before the next instance of the delimiter character delim (newline \n by default), or when either stream ends. If delim was present in the input, it remains available as if unread.

Method: istream& istream::getline (charptr, int len [, char delim])

Read a line from the input stream, into the array at charptr. charptr may be any of three kinds of pointer: char*, unsigned char*, or signed char*.

The remaining arguments limit how much to read: up to (but not including) the first occurrence in the input of a line delimiter character delim---newline (\n) by default, or up to `len-1' characters (or to end of file, if that happens sooner).

If getline succeeds in reading a "full line", it also discards the trailing delimiter character from the input stream. (To preserve it as available input, see the similar form of iostream::get.)

If delim was not found before len characters or end of file, getline sets the ios::fail flag, as well as the ios::eof flag if appropriate.

getline writes a null character at the end of the string, regardless of which condition terminates the read.

Method: istream& istream::read (pointer, int len)

Read len bytes into the location at pointer, unless the input ends first.

pointer may be of type char*, void*, unsigned char*, or signed char*.

If the istream ends before reading len bytes, read sets the ios::fail flag.

Method: istream& istream::gets (char **s [, char delim])

A GNU extension, to read an arbitrarily long string from the current input position to the next instance of the delim character (newline \n by default).

To permit reading a string of arbitrary length, gets allocates whatever memory is required. Notice that the first argument s is an address to record a character pointer, rather than the pointer itself.

Method: istream& istream::scan (const char *format ...)

A GNU extension, similar to fscanf(file, format, ...). The format is a scanf-style format control string, which is used to read the variables in the remainder of the argument list from the istream.

Method: istream& istream::vscan (const char *format, va_list args)

Like istream::scan, but takes a single va_list argument.


Go to the first, previous, next, last section, table of contents.