1 / 31

I/O Streams

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.

Download Presentation

I/O Streams

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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;

  10. Formatted istream Extraction • The correct code would be: cin >> dec >> month >> slash >> day >> slash >> year;

  11. 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.

  12. 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)) { • }

  13. 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, ’,’);

  14. 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.

  15. 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.

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

  17. 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.

  18. 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.

  19. 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.

  20. 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.

  21. 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.

  22. 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.

  23. 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.

  24. 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.

  25. 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.

  26. 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.

  27. 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).

  28. ostream Example • Formatting integer output. • Formatting floating point output.

  29. 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.

  30. Tying an istream to an ostream • By default, cout is synchronized to cin. • This ensures that the cout statements appear before the subsequent input.

  31. 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.

More Related