"C++ Primer" reading notes-Chapter 8 IO library

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
Insert picture description here

2. Conditional status of IO stream

Insert picture description here
You can use the loop to determine the flow condition to read the file
Insert picture description here

3. Manage output buffers

Insert picture description here
endl、ends、flush

8.2 File input and output

1. The unique operation of fstream

Insert picture description here

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.
Insert picture description here

3. You can use subclasses instead of parent classes

Insert picture description here
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

Insert picture description here

6. File mode

Insert picture description here
Insert picture description here

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
Insert picture description here
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

Insert picture description here

2. Unique operation

Insert picture description here

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.
Insert picture description here

Guess you like

Origin blog.csdn.net/youyadefeng1/article/details/114121575