Eighth Chapter: IO Library
Notes
1. The C + + language does not directly handle input and output, but rather handles IO through a family of types defined in the standard library. These types support IO operations that read data from the device, write data to the device, and the device can be files, console windows, memory , and so on.
2. The Getline function reads a row of data from a given IStream into a given string object. Such as:
string Line ; // read a whole line each time until the end of the file is reached (getline (CIN, line )) << lines <& Lt Endl
3. The standard library allows us to ignore the differences between these different types of streams, which are implemented through inheritance mechanisms .
4. The IO object is not copied or assigned .
5. Using the good or fail function of a stream is the correct way to determine the overall state of the flow.
6. Each output stream manages a buffer that is used to store the data read and written by the program.
7. With the buffering mechanism, the operating system can combine multiple output operations of a program into a single system write operation. Because the write operation of a device can be time-consuming, allowing the operating system to combine multiple output operations into a single device write can result in a significant performance boost.
8. By default,cin and Cerr are associated to cout, so either read cin or write Cerr will cause the cout buffer to be flushed.
9. It is often a good practice to test whether open is successful.
10. Automatic Construction and destruction:
//perform a looping operation for each file passed to the program for(Auto p = argv +1; P! = argv + argc; ++p) {ifstream input (*P);//create an output stream and open a file if(input) process (input); ElseCerr<<"couldn ' t open:"+string(*P); } //Each loop step input will leave the scope and will be destroyed//because input is a local variable in the while loop, it is created and destroyed once in each loop step. //when a FStream object leaves its scope, the file associated with it is automatically closed (call Close ()).
One. opening a file in out mode discards existing data .
The Sstream header file defines three types to support memory IO, which can write data to a string and read data from a string, just as a string is an IO stream.
STRM.STR () Returns a copy of the string saved by STRM.
14. When some of our work is to process the entire line of text, while others work on a single word within the line, you can usually use Istringstream.
Terms
Inheritance mechanism (inheritance), conditional State (condition), file mode, file stream
2016-11-04 20:33:23
C + + Primer 5th notes: Eighth chapter