C ++ "input and output stream and file system", input and output
Concept of I/O Stream1. Stream1. Meaning: a stream is a transmission byte sequence and a "flow" of byte between objects. stream operations include input and output.
2. Role: provides a flexible and easy-to-use Input and Output Mechanism. input and output are data transmission processes, and data flows from one location to another like a stream.
2. Common streams1. cin is a standard input stream object, used with the extraction operator> for input. 2. cout is the standard output stream object, used with the insert operator <for output.
Stream format ControlI. Features of format operators and member functions1. Similarities: (1) format operators and member functions control the format by setting the format status word.
(2) The two functions are the same and equal.
(3) They are used together in the same program.
2. Differences: (1) different methods are used, and operators are directly used in the stream. member functions are called through objects.
(2) function-based operators should include the iomanip header file.
File Input and OutputI. File Creation1. file input: reads data from a file to the memory.
2. file output: refers to the data output from the memory to the file.
3. creation process: (1) create a file stream object for the input and output of the file; (2) link the stream object to the opened file; (3) then read and write the file; (4) close the file after reading and writing is complete;
When opening a file or reading or writing a file, you must use the stream error detection function or the error status to ensure that the file operation is correct.
Ii. file inputWhen reading data from a file to memory, text files are input with Common Extraction operators>. In file input, you must always check that the file enters the end. The input stream's member function eof () it is used to detect whether the end of the file is reached. If the end of the file is read, true is returned.
Iii. file output1. Create an input file stream (object) and link the file opened in the input mode to the file stream (check whether the file is opened successfully ).
2. Read the content from the input file stream (judge whether the read is successful ).
3. close the file (the file will be automatically closed when the object disappears ).