1 / 14

C++ I/O Concepts: Streams, Methods, and File I/O

Learn about C++ I/O, streams like istream and ostream, methods for extraction and insertion, file I/O with fstream, and reading/writing data to/from files.

blaclair
Download Presentation

C++ I/O Concepts: Streams, Methods, and File I/O

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. C++ I/O Dick Steflik CS-212

  2. C++ I/O • Modeled after UNIX’s concept of a “stream” • conceptionally a stream is a continuous flow of characters/bytes from one place to another • an abstraction that is used to hide all of the details of doing I/O programming from the user • ostream – characters flow from the program to an output device • istream – characters flow from an input device to the program

  3. istream • istream – the C++ class used for inputting information from an arbitrary device (keyboard, hard drive, diskette…) to a program. • C++ has an always instantiated object of class istream called “cin” • by default cin represents your keyboard or your console input device (stdin)

  4. istream (cont) • since istream is a class it also has a set of methods • extraction – to take information from the input device and assign it to a program variable the “>>” (shift right) operator has been overloaded and named extraction; it extracts information from the input stream. • program execution is “blocked” until data is ready to be read. • ex. int age,time; cin >> age >> time; • leading “white space” is ignored by extract • extraction rules are based in “type”

  5. istream – state methods • return the state of the istream status flags • good( ) • true – all is ok with the stream • false – an error was encountered • bad( ) • true – an unrecoverable error occurred • false – OK • fail( ) • true – an unrecoverable error occurred in the stream • false – everything is normal • eof( ) • true – an end-of-file mark was encountered • false – normal • clear( ) – used to clear the stream after an error is detected

  6. istream – other methods • get(char ) – read a single character (will read white space) • get(chArray,N,stop) reads characters into chArray until n chars have been read –or- the stop char is found • getline( chArray,n,stop) – same but stop is not read and is left in the stream • read( chArray,n) – read into chArray until n chars or eof. • readsome(chArray,n) – same but returns number of chars extracted • peek( ) – return the next char in the stream but leave it there. • ignore(n,stop) – skip n chars in stream or until stop is encountered • width( n ) – set max chars to be read to n • putback( ch) – put the character ch into the stream • unget( ) – put the most recent character read back into the stream • seekg(offset , base) – move the read position fo offset from the last base set by ios::beg, ios::cur or ios::end • tellg( ) – return position (offset) of the current read position.

  7. ostream • ostream – the C++ class used for outputting information to an arbitrary device (screen, hard drive, diskette…) from a program. • C++ has an always instantiated object of class istream called “cout” • by default cout represents your screen or your console output device (stdout)

  8. ostream (cont) • since ostream is a class it also has a set of methods • insertion – to send information from a program element to an output device ; the “<<” (shift left) operator has been overloaded and named insertion; it inserts information into the output stream. • ex. int age,time; cout << age << “ “ << time; • insertion formatting rules are based in “type” but can be overridden using formatting manipulators

  9. noshowpos boolalpha noboolalpha uppercase nouppercase unitbuf flush endl left right internal Format manipulators • fixed • scientific • showpoint • noshowpoint • dec • hex • oct • showbase • noshowbase • showpos

  10. File I/O • fstream – parent class for ifstream • ifstream – input file stream • ofstream – output file stream

  11. fstream methods • open( filename) – attach a file to an fstream • close( ) – close the stream • is_open() – boolean; true if open, false if not • eof() – boolean; true if at end of file , false if not • File opening modes • ios::in – open file for input • ios::trunc – open and delete contents • ios::out – open for output (uses trunc) • ios::app – open for output at the end of the file • ios::ate – open for reading at end of the file • ios::binary – open for binary I/O using read( ) and write( )

  12. Reading and writing to/from a file • Declare in input stream: • ifstream in; • Declare an output file stream • ofstream out; • attach the input stream to as file • in.open(“myfile.txt”); • attach the output stream to a file • out.open(“myoutputfile.txt”); • or – combine the steps • ifstream in(“myfile.txt”); • ofstream out(“myoutputfile”);

  13. cont. Reading/Writing • assuming input file is full of integers (separated by blanks) for (;;) // repeat forever { in >> myint; //read an int if ( in.eof()) break; cout << myint << endl; // print on the console out << myint; // write to the output file }

More Related