8th standard IO library
8.1 Object-oriented standard library
1. The IO type is defined in three separate header files: iostream defines the type of read-write control window, fstream defines the type to read and write to the named file, and the type defined by Sstream is used to read and write to the string object stored in memory. The types of beauty that are defined in FStream and Sstream are derived from the related types defined in the iostream header file.
2. Stream objects cannot be copied and therefore cannot be stored in a vector container
3. The parameter or return type cannot be a stream type either. If you need to pass or return an IO object, you must pass or return a pointer or reference to that object.
8.2 Item Status
The IO standard library manages a set of conditional state members to mark whether a given Io object is in the available state, or what particular error is encountered.
1. s.eof (); S.fail (); S.bad (); S.good (); S.clear (); S.clear (flag); S.setstate (flag); S.rdstate ()
2. Analyze the case where the while (Cin>>val) error occurred?
8.3 Management of output buffers
The following situations will cause the contents of the buffer to be flushed, that is, to write to the actual output device or file:
(1) The program ends normally.
(2) in some uncertain times, the buffer may be full.
(3) Flush buffer with modifier explicitly type, such as line Terminator Endl. such as:cout<<flush; cout<<ends; cout<<endl;
(4) After each output operation is finished, set the internal state of the stream with the unitbuf operator. such as:cout<<unitbuf<<nounitbuf; Equivalent to cout<<flush;
(5) The output stream can be associated with the input stream.
8.4 Input and output of the file
1. Check if File Open is successful
Ifstream Infile;ofstream Outfile;infile.open ("in"), OutFile ("outfile"), if (!infile) {cerr<< "error:unable to Open input file ";}
2. Re-bundle the file stream with the new file
Ifstream infile ("in"); Infile.close (); Infile.open ("Next");
3. Clear the status of the file stream
Inflie.clear ();
4. File mode
In open file for read operation
Out open file do write operation
The app finds the end of the file before each write
Ate to locate the file at the end of the file immediately after opening the file
Trunc empty an existing file stream when opening a file
Binary IO operation for Forbidden mode
For example:
Ofstream outfile1 ("File1"); Ofstream outfile2 ("File1", Ofstream::out | ofstream::trunc); ofstream outfile3 ("File3", Ofstream::app);
Input and output operations on the same file
FStream inOut ("Copyout", Fstream::in | fstream::out);
Schema is a property of a file rather than a stream
8.5 string Stream
Sstream header file with Istringstream and ostringstream two classes for read and write operations, respectively
C + + Primer reading notes: 8th standard IO Library