Print service provided by iDogiCat: http://www.idogicat.com/
home logo





Home > IT > Programming > C++ Programming > iDog's Interview C++ > iDog's Interview C++ Part 6

iDog's Interview C++ Part 6

Stream, I/O

Stream

One principal goal of streams is to encapsulate the problems of getting the data to and from the disk or the screen. Once a stream is created, your program works with the stream and the stream takes care of the details.

  • streambuf - manages buffer
  • ios - base class of input/output stream classes, containing a streambuf object.
  • istream & ostream - derived from ios
  • iostream - derived from istream & ostream, with functions for writing to screen.
  • fstream - file IO.
  • sstream - string IO.

Standard I/O Objects

When a C++ program that includes the iostream classes starts, following four objects (and also the 'w' versions of them) are created and initialized:

  • cin - input from keyboard
  • cout - output to screen
  • cerr - output to screen, unbuffered
  • clog - buffered error messages to screen. It's common to redirect it to a log file.

The difference between cerr and clog is that cerr is unbuffered, while clog is buffered. Also, a corresponding wide character version for each of the above four stream classes is provided:

  • wcin
  • wcout
  • wcerr
  • wclog

cin

cin regards spaces as seperators.

cin.get(char&) return a character from input stream:


cin.get(c1).get(c2).get(c3); 
cin.get(c1, 256, '\n');  // get max 256 chars, seperated by '\n' (default seperator).
                         // Termination char is kept. 

cin.getline(c1, 256); // termination char is discarded. 

cin.ignore(80, '\n'); // ignores 80 chars until get a new line. 
                      // NL char will be discarded. 

cin.peek() // look at the next char without extracting it 
cin.putback(c1); // insert a char in cin. 

cout

Flush cout: any one of the follows.


cout.flush(); 
cout << flush; 

cout.put(char): write a char to cout


cout.put('a').put('b'); 

cout.write(char*, count); 


// #include <iomanip>

cout.width(count); // modify the width of the next output 

cout.fill(char)  // use the given char to fill if the object is not 
                 // as wide as the width set by function cout.width().

// Eg:

cout.width(5);
cout.fill('*');
cout &lt;&lt; 12;

// shows: "***12" (without quotation marks)

Set flags of stream object


cout.setf(flag);
where flag can be:
  • ios::showpoint - show trailing zeros
  • ios::showpos - show '+' for positive numbers
  • ios::left, ios::right, ios::internal - alignment
  • ios::dec, ios::oct, ios::hex

these flags can also be concatenated into the insertion operator:


cout &lt;&lt; setw(10) &lt;&lt; hex &lt;&lt; 20;

clog:

Usage: redirect it to a log file:


ofstream out("my.log");

if(out)
    clog.rdbuf(out.rdbuf());

clog << "aaaaaaaa" << endl;

File I/O

Header file: <fstream>


ofstream ofs(filename, flag);
Flags:
  • ios::app - append
  • ios::at - put file pointer at the end of file, but data can be written at anywhere in the file
  • ios::trun - default value. existing file truncated (cleared)
  • ios::nocreate - if file does not exist, then the open fails
  • ios::noreplac - if file already exists, then the open fails

ifstream ifs(filename, flag);

  • ios::binary

Anything (including objects) can be written to a binary file by using cout.write((char*)obj, size) function; it then can be recovered by using cin(refOfObj, size) function.


MyObj myObj(0);
ofstream ofs("aaa", ios::binary);
ofs.write((char*)myObj, sizeof(myObj));
ofs.close(100);

MyObj myObj1;
ifstream ifs("aaa", ios::binary);
ifs.read(myObj1, sizeof(myObj1);
ifs.close();

String Stream

Use sstream to convert data to or from string.


#include &lt;sstream>

using namespace std;

int main()
{
    stringstream ss1("123");
    int i;
    ss1 >> i;    // parse string to int
    cout << i << endl;

    stringstream ss2;
    ss2 << "output: " << i;  // convert int to string
    cout << ss2.str() << endl;

    return 0;
}