C++ Basics Review 2

IO library

istream (input stream) type, which provides input operations;
ostream (output stream) type, which provides output operations;
cin: an istream object, which reads data from standard input;
cout: an ostream object, which reads data from standard output;
cerr: An ostream object, usually used to output program error messages, written to standard error;
">>": operator, used to read data from an istream object;
"<<": operator, used to read data from an ostream object Read data;
getline function, read a line of data from a given istream object and store it in a given string object.

C++ uses standard library classes to handle stream-oriented input and output

  • iostream handles console IO
  • fstream handles command file IO
  • The IO classes fstream and stringstream that complete the memory string of
    stringstream inherit from iostream, the input class inherits from istream, and the output class inherits from ostream; the operations that can be performed on the istream object can also be performed on the ifstream or istringstream object. Inherited from the ostream output class in the same way.

IO objects are not copied or assigned

 ofstream out1, out2;
 out1 = out2; //错误:不能对流对象赋值
 ofstream print(ofstream);//错误:不能初始化ofstream参数
 out2 = print(out2); //错误:不能拷贝流对象

Since the IO object cannot be copied, we cannot set the formal parameter or return type to the stream type. Functions that perform IO operations usually pass and return streams by reference. Reading and writing an IO object will change its state, so the passed and returned References cannot be const.

condition status

IO library conditional state
strm:iostate: strm is an IO type;
strm:badbit: used to indicate that the stream has crashed;
strm:failbit: used to indicate that the IO operation failed;
strm:eofbit: used to indicate that the file has ended;
strm: goodbit: used to indicate that the stream is not in an error state;
s.eof(): returns true if the eofbit of the stream s is set;
s.fail(): returns true if the failbit or badbit of the stream is set;
s. bad(): Returns true if the badbit of the stream is set;
s.good(): Returns true if s is in a valid state ; s.clear():
Resets all conditional status bits in the stream s, The status of the stream is set to be valid and void is returned;
s.clear(flags): According to the given flags flags, reset the corresponding condition status bits in stream s.
s.setstate(flags): Set the corresponding conditional state bit in stream s according to the given flags flag bit.
s.rdstate(): Returns the current conditional state of stream s.

flush output buffer

cout << "hi" << endl; //输出hi和一个换行,然后刷新缓冲区
cout << "hi" << flush; //输出hi,然后刷新缓冲区,不附加任何额外的字符
cout << "hi" << ends; //输出hi和一个空字符,然后刷新缓冲区

Warning: If the program crashes, the output buffer will not be flushed.
This point is deeply felt when viewing bugs according to the log. Sometimes the program crashes, but the data it outputs may stay in the output buffer waiting to be printed.

file mode

Each stream is associated with a file mode that indicates how to use the file.
in: open in read mode
out: open
app in write mode: locate to the end of the file before each write operation
ate: locate the end of the file immediately after opening the file
trunc: truncate the file
binary: perform IO in binary mode

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325928230&siteId=291194637