31
I/O Streams • C++ I/O occurs in streams that are simply a sequence of bytes. Low-level I/O transfers some number of bytes where the individual byte is the item of interest. High-level I/O transfers information as meaningful units such as integers, floating-point numbers, characters, strings, etc.

I/O Streams

Embed Size (px)

DESCRIPTION

I/O Streams. C++ I/O occurs in streams that are simply a sequence of bytes. Low-level I/O transfers some number of bytes where the individual byte is the item of interest. - PowerPoint PPT Presentation

Citation preview

I/O Streams

• C++ I/O occurs in streams that are simply a sequence of bytes.– Low-level I/O transfers some number of

bytes where the individual byte is the item of interest.

– High-level I/O transfers information as meaningful units such as integers, floating-point numbers, characters, strings, etc.

istream

• The istream library supports stream-input operations including cin.– Input from the istream object is called

extraction because characters are being pulled out of the stream into the program.

istream

• Each istream object has an error state and several formatting values.– Errors include reaching the end of file,

formatting errors such getting letters when digits are expected, and serious errors such as file not found.

istream

• The extraction steps are:– Check the stream’s error state.

• If it is non-zero then there is some error.

– If the extraction is from cin then the cout stream is flushed.

– Any leading whitespace is skipped.– The characters are extracted to obtain the

value. • Whitespace terminates the extraction.

istream errors

• eofbit - end of file was reached– EOF is set when the program attempts to

read one character past the last character.– If EOF is found while skipping leading

white space the failbit is set.

• failbit – extraction failed– EOF reached while skipping white space or

wrong type of characters encountered.

istream Errors• badbit – the stream is bad and can not be

used.– cin.good() – none of error bits are set.– cin.eof() – eofbit is set.– cin.fail()- failbit or badbit is set.– cin.bad()- badbit is set.

• Note: Test the stream after completing an extraction, not before.

Formatted istream Extraction

• The >> operator is used for formatted extractions.

• Extraction operators are overloaded for the following types:– char – single character– char * - A sequence of characters– short, long, int – integer values– float, double – floating point values.

Formatted istream Extraction

• Format manipulators– If a value begins with 0 (zero), it is

assumed to be an octal number.– If a value begins with 0X or 0x, then the

number is assumed to be a hexadecimal number.

– Otherwise, the value is assumed to be a decimal number.

Formatted istream Extraction

• What is the issue with the following code that reads dates in the form mm/dd/yy?

int month, day, year;

char slash;

cin >> month >> slash >> day >> slash >> year;

Formatted istream Extraction

• The correct code would be:

cin >> dec >> month >> slash >> day >> slash >> year;

Unformatted istream Extraction

• Unformatted extraction means that:– The bytes are copied. There is no

conversion to data types.– Leading spaces are not skipped.– Calls are made to member functions to

extract data rather than overloaded operators.

Unformatted istream Extraction

• The get() function extracts one character at a time from the input stream.

char letter;

cin.get(letter);

while (letter != EOF) {

cin.get(letter);

}

char letter;

while (cin.get(letter)) {

}

Unformatted istream Extraction

• The get(bufferName, BUFFER_LENGTH) reads characters up to (but not including) a particular delimiter.– Input is read until the buffer is full or the delimiter character is

found.

const int BUFF_LEN = 80;

char inputData;

cin.get(inputData, BUFF_LEN, ’,’);

Unformatted istream Extraction

• The getline(bufferName, BUFFER_LENGTH) function extracts one line at a time.– This is similar to:

• get(bufferName, BUFFER_LENGTH, ‘\n’);

– getline discards the ‘\n’ character.

istream functions• The ignore function skips over a

designated number of characters or terminates upon reaching a designated delimiter.– The default number of characters to skip is 1.

• The putback function places the previous character obtained by a get back into the stream.

istream Functions

• The peek function returns the next character from the input stream but does not remove the character from the stream.

ostream

• The ostream library supports stream-output operations including cout.– Output to the ostream object is called

insertion because characters are being inserted into the stream by the program.

ostream

• Each ostream object has an error state and several formatting values.– Errors include reaching the end of file, and

serious errors such as file not found.– Formatting state controls the details of the

conversions for formatted insertions.

ostream

• The insertion steps are:– The stream’s error is checked.– The value is converted to the appropriate

characters, possibly padded, and inserted into the stream.

ostream Format State

• The format state consists of flags and three additional values: the fill character, precision, and width.– By default, no flags are set, the fill

character is space, the width is 0, and the precision is 6.

ostream Format State

• The format flags can be manipulated using the following:– The flags() function returns the current flag

settings.– The flags(int f) replaces the current flag

settings with f and returns the previous flag settings.

ostream Format State– The setf(int f) turns on all flags given in f and

returns the previous flag settings.• Any flags not indicated in f are left in their current

state.• This is a bitwise or of the current flag settings and f.

– The unset(int f) turns off all flags given in f and returns the previous flag settings.

• Any flags not indicated in f are left in their current state.

• This is a bitwise and of the current flag settings and the complement of f.

ostream Format State

• Other values can be manipulated with the following functions:– The fill() function returns the current fill

character.– The fill(char c) function sets the fill character

to c and returns the previous value.– The precision() function returns the current

precision.

ostream Format State

– The precision(int p) function sets the precision to p and returns the previous value.

– The width() function returns the current width.

– The width(int w) sets the width to w and returns the previous value.

ostream Format State

• The following functions are used to manipulate the output:– cout << dec - sets all output to be decimal

values.– cout << oct - sets all output to be octal.– cout << hex - sets all output to be

hexadecimal.– cout << flush - flushes the output buffer.

ostream Format State

• Additional manipulator functions are available in the iomanip.h file. – cout << setw(w) - sets the width of the

output to w.– cout << setfill(c) – sets the fill character to

c.– cout << setprecision(p)- sets the precision

to p.

ostream Format State

– cout << setiosflags(f) – sets the flags just as the setf(f) function does.

– Cout << resetiosflags(f) - turns off all flags in f in the same manner as unsetf(f).

ostream Example

• Formatting integer output.

• Formatting floating point output.

Unformatted ostream Insertion

• Unformatted insertions only transfer bytes with no conversion. – Member functions are used to insert data

rather than overloaded operators.

– The cout.put(ch) function writes a single byte.

– The cout.write(buffer, length) function writes an array of bytes.

Tying an istream to an ostream

• By default, cout is synchronized to cin.– This ensures that the cout statements

appear before the subsequent input.

Tying an istream to an ostream• The tie function permits the programmer to

synchronize an ostream with an istream.

inputStream.tie(outputStream); – Ties the outputStream to the inputStream.

inputStream.tie(0);– Unties the outputStream from the inputStream.