1 / 21

I/O in C++

October 7, 2008. Junaed Sattar. I/O in C++. Stream I/O. a stream is a flow of bytes/characters/ints or any type of data input streams: to the program output streams: from the program note I use plural one program can have multiple I/O streams associated and vice-versa. Input/Output.

Download Presentation

I/O in C++

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. October 7, 2008. Junaed Sattar I/O in C++

  2. Stream I/O • a stream is a flow of bytes/characters/ints or any type of data • input streams: to the program • output streams: from the program • note I use plural • one program can have multiple I/O streams associated • and vice-versa

  3. Input/Output • Console based, no GUI • standard streams: • cin: standard input • cout: standard output • cerr: standard error

  4. Extraction/Insertion cout << “Hello world!”; cout << “The value of i is “ << i << endl; //endl puts a new line cout << “Please enter your name: “; string name; cin >> name;

  5. What are cin and cout? • Stream classes • Classes have methods, as we know • so does cin and cout • some common to all I/O stream classes in C++ • File I/O, binary/text mode I/O, console I/O

  6. One example • cin inputs ints, chars, null-terminated strings, string objects • but terminates when encounters space (ascii character 32)‏ • workaround? • use the “get” method

  7. Inputting “i am oh so cool” cin.get gets the entire line just cin will get “I” space termination Snippet char tData[100]; // This is a method in C++ istream classes for // inputting text //including spaces cin.get( tData, 99 ); // or cin.get(tData,99,'\n');

  8. Or, • Use the getline function getline( cin, name );

  9. File I/O • Reading from or writing to files on disk • ifstream and ofstream classes • dedicated for input and output respectively • or, use fstream

  10. Example Files Program(filesdemo) ofstream myofile; myofile.open( “sample.txt” ); myofile << “This is a sample line I'm writing\n”; myofile.close(); ... ifstream myifile; myifile.open( “sample.txt” ); string oneLine; getline( myifile, oneLine ); cout << oneLine; myifile.close();

  11. Read/Write to files (files1/2)‏ • Similar to how we use cin and cout • remember, these are I/O streams too • myfile is a file stream object, then: • to write an int: • int i = 10;myfile << i; • to read an int: • int i;myfile >> i;

  12. Binary files • As opposed to text files, they are unformatted as ascii. • text files stores everything as ascii text strings • even numbers • binary files do not • Example: consider outout of the program in the previous slide

  13. Difference? • Example program • Accepts student ID (I input 1010) • Accepts name (I input Junaed) • Accepts CGPA (I input 4.5) • Save into two files, as text and binary

  14. Storage TEXT FILE BINARY FILE

  15. Binary files • Files by default are text • Different methods to write and read • requires casting (we'll see casting soon)‏ • different data format • If time permits, we'll revisit

  16. Failures? • If open fails? • Check before use • if( !myifile ){ cerr << “Cannot open file!”; exit(1);} • End of file? • while( myifile.fail() ){ //do your operations here}

  17. Random vs Sequential • Random access files • nonsequential, • as a result faster access times, • content must be suitable for random access • for example. not on network streams! • or console input

  18. File “heads” • Access positions • one each for read and write • hence two methods: • seekg (as in “get”) for reading • seekp (as in “put”) for writing • ifstreams have seekg • ofstreams have seekp

  19. seeking • seekg( position, mode) //(same for seekp)‏ • position is a long integer signed offset • mode can be • ios::beg: from the beginning • ios::end from the end • ios::cur from current position

  20. telling • tellg and tellp • returns as long integer, the position of the get and put positions, respectively

  21. example seeks file.seekg( 20L, ios::beg ); file.seekp( -100L, ios::cur ); long pPosition = file.tellp(); long gPosition = file.tellg();

More Related