Chapter 8 IO Library
8.1 IO class
1. No copy or assignment of IO objects
We cannot copy or assign values to IO objects
2. Conditional status of IO stream
You can use the loop to determine the flow condition to read the file
3. Manage output buffers
endl、ends、flush
8.2 File input and output
1. The unique operation of fstream
2. Create a file stream
When creating a file stream, if you give the constructor a default file name parameter, it will assume that the open function has been called by default. Otherwise, you need to call the open function after the association.
3. You can use subclasses instead of parent classes
fstream inherits from iostream, so you can use fstream as a function parameter and pass it into a function that requires iostream parameters
4、open和close
Only after closing the currently associated file with close, can you use open to associate the new file again
. It is a good habit to check whether the open is successful or not.
5. Automatic construction and destruction
6. File mode
7. The default mode of ofstream is out || trunc
Whenever a file is opened with ofstream, it will be opened in output truncation mode, and the contents of the file will be lost.
If you want to turn off the truncation mode, you must open
it in app mode. Note that ofstream::app is used here, not fstream::app
8.3 string stream
Need to use sstream header file
1. Basic usage
2. Unique operation
3. The usage of isringstream
When inputting a line of character string and need to slice the line of character string, using isringstream is an excellent choice.