Many people use the FILE type pointer FILE under ansi c in C ++ programming to execute FILE-related operations, which inevitably affects the overall aesthetics of the C ++ program. In addition, the C ++ standard IO Library provides a more complete and easy-to-use file operation method. Therefore, we recommend that you use the standard library to implement file input/output and cultivate a good programming habit.
1. File Input and Output
The header files for file operations include: ifstream provides the File Reading function; ofstream provides the file writing function; fstream provides the File Reading and Writing Function (including the first two types of functions, read/write operations on the same file ).
A. Define the object
ifstream infile; ofstream outfile;
B. Before using fstream objects, you must bind these objects to the files to be read and written.
infile.open (); outfile.open ();
Of course, the fstream class provides constructor, which can implement the above two steps during initialization when defining objects.
ifstream infile (); ofstream outfile ();
C. Check whether the file is successfully opened.
(!<< << -
D. File Content Processing
I. The member function eof () is used to check whether it has reached the end of the file. If it has reached the end of the file, a non-0 value is returned; otherwise, 0 is returned.
(!
II. files can be divided into text files and binary files. The process of text files is relatively simple, and you can use the plug-in and analysis tool. The process of binary files is more complex, the following two methods are described respectively.
Text File: output to the file with the plug-in (<), and input from the file with the plug-in (>. Assume that infile is opened as input, and outfile is opened as output.
outfile<<>>word;
This method also provides a simple formatting capability, such as specifying the output as hexadecimal.
Operator |
Function |
Input/Output |
Dec |
Formatted as a decimal Value |
Input and Output |
Hex |
Formatted as a hexadecimal value |
Input and Output |
Oct |
Formatted as octal numeric data |
Input and Output |
Setpxecision (int p) |
Set the number of precise digits of a floating point. |
Output |
Endl |
Output A linefeed and refresh the stream |
Output |
Ends |
Output an empty character |
Output |
For example, to output 123 as a hex: outfile Getline function, one line of input file, function prototype
istream& getline (istream& , & str, & getline (istream& , & str);
Read the string from is and store it in str until the delimiter delim is encountered. For the second form, getline encounters '\ n' stop. During execution, if delim appears, it will be read and discarded, and the next input will start after it.
Binary File: The put () function writes a character to the stream. Its prototype is ofstream & put (char ch), which is also relatively simple to use, such as outfile. put ('C'); is to write a character 'C' to the stream '.
The get () function is flexible and has three common overload methods:
One is the form corresponding to put (): ifstream & get (char & ch); the function is to read a character from the stream and save the result in the reference ch, if it is at the end of the file, null characters are returned. For example, infile. get (x); indicates to read a character from the file and save the character to x.
The prototype of another form of overload is: int get (); this form returns a character from the stream. If it reaches the end of the file, it returns EOF, for example, x = file2.get (); the function is the same as that of the previous example.
Another prototype is ifstream & get (char * buf, int num, char delim = ''). In this form, the characters are read into the array pointed by the buf, the default value of the Line Break ''is used if the num character is read or the character specified by delim is encountered ''.
To read and write binary data blocks, you must use the member functions read () and write (). Their prototype is as follows:
read (unsigned *buf, unsigned *buf, num);
III. file Location. C ++ file location can be divided into read location and write location. The corresponding member functions are seekg () and seekp (). seekg () is used to set the read location, seekp is used to set the write position. Their most common forms are as follows:
istream &&seekp (streamoff offset, seek_dir origin);
Offset defines the offset. seek_dir indicates the reference position for moving. It is an enumeration with the following values:
Ios: beg: Start of the file
Ios: cur: current file location
Ios: end of the file
These two functions are generally used for binary files (bytes), because text files may be different from the expected values due to the system's interpretation of characters.
file1.seekg (,ios::cur); file2.seekp (,ios::beg);
E. close the file
When the program ends or the fstream object is associated with another file, the current file must be closed first.
infile.close();
2. File Mode
When opening a file, you must specify the file mode whether to call open or use the file name as a part of stream initialization. Both the file stream constructor and open function provide default parameters to set the file mode. The default value varies depending on the stream type. The common file modes and meanings are as follows:
File Mode |
Description |
In |
Open a file for read Operations |
Out |
Open a file for write operations |
App |
Find the end Of the file before each write |
Ate |
Locate the file at the end of the file when opening the file |
Trunc |
Clear an existing file stream when opening the file |
Binary |
Perform IO operations in binary mode |
A stream opened in binary mode processes the file in a byte sequence without interpreting the characters in the stream.
By default, the files associated with the ifstream stream are opened in mode and the files associated with the ofstream are opened in out mode. Files opened in out mode are cleared and all data stored in the file is discarded. In effect, the out mode specified for the ofstream object is equivalent to the out and trunc modes specified.
Mode Combination. Multiple File modes can be specified at the same time, but some combinations are meaningless. The common combination modes are as follows:
Combination Mode |
Description |
Out | app |
Open the file and write it at the end of the file. |
Out | trunc |
Open the file for write operations, delete existing data in the file (same as out) |
In | out |
Open the file and perform read and write operations. Locate it at the beginning of the file. |
In | out | trunc |
Open the file for read and write operations, and delete existing data in the file |
In | out | ate |
Open the file and perform read and write operations. Locate it at the end of the file. |
...... |
...... |