CPP stream input and output library

The most important of the three output streams

  • ostream
  • ofstream
  • ostringstream

Output stream predefined objects

  • cout standard output
  • cerr standard error output, no buffer, its content is sent to the output immediately.
  • cerr, similar to clog, but there is a buffer, when the output buffer is full.

Standard output commutation

ofstream fout("b.out");//自动打开文件,并且绑定到fout这个对象
streambuf*  pOld  =cout.rdbuf(fout.rdbuf());  cout的绑定到fout,而不是标准输入输出设备(显示器)
//…
cout.rdbuf(pOld);//绑定会原来的fout标准输入输出设备

Constructing the output stream object

  • ofstream class supports disk file output
  • If you specify a file name in the constructor, when the structure of the document file is opened automatically

    ofstream myFile("filename");
    
  • You can use open member function to open a file after calling the default constructor

    ofstream myFile; //声明一个静态文件输出流对象
    myFile.open("filename");   //打开文件,使流对象与文件建立联系
    
  • Mode can be specified when the object is constructed, or open the file open

    ofstream myFile("filename", ios_base::out | ios_base::binary);
    

Three types of file output stream member function

  • The actuating member function equivalent character.
  • Write operation member functions unformatted.
  • Other modifications and different flow states or manipulator insertion member function operator.

File output stream member function

  • open function

The flow associated with a particular disk file up.
You need to specify the open mode.

  • put function

Put a character to the output stream.

  • write function

A piece of the contents of memory to a file output stream

  • seekp and tellp function

Internal pointer file stream operation

  • close function

Close the stream associated with a file output disk file

  • Error handling function

Error handling when written to a stream

Guess you like

Origin www.cnblogs.com/working-in-heart/p/12232549.html