210 likes | 344 Views
This guide explores the fundamentals of input/output (I/O) streams in C++, including how to utilize standard streams like cin, cout, and cerr for console-based operations. It covers file I/O, detailing methods for reading and writing to text and binary files using the ifstream and ofstream classes. Additionally, it discusses advanced topics such as random access in files, seeking positions for reading and writing operations, and handling potential file failures. With practical examples, this guide enables you to effectively manage data flow in your C++ applications.
E N D
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 • Console based, no GUI • standard streams: • cin: standard input • cout: standard output • cerr: standard error
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;
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
One example • cin inputs ints, chars, null-terminated strings, string objects • but terminates when encounters space (ascii character 32) • workaround? • use the “get” method
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');
Or, • Use the getline function getline( cin, name );
File I/O • Reading from or writing to files on disk • ifstream and ofstream classes • dedicated for input and output respectively • or, use fstream
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();
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;
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
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
Storage TEXT FILE BINARY FILE
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
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}
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
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
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
telling • tellg and tellp • returns as long integer, the position of the get and put positions, respectively
example seeks file.seekg( 20L, ios::beg ); file.seekp( -100L, ios::cur ); long pPosition = file.tellp(); long gPosition = file.tellg();