1, open the file.
There are two ways to open a file. The first method is to use the constructor of the fstream class. The fstream file ("test.dat", IOs _ base:: in | IOs _ base:: out | IOs _ base:: app); Another way is to use.
Open function. Fstream file; file.open("test.dat ",IOs _ base::in | IOs _ base::out | IOs _ base::app);
So you can open a read-write file. If the file does not exist, a new file will be created and opened in read-write mode. It should be noted here that if the file does not exist, the second parameter in the open function must contain ios_base::out|ios_base::app, otherwise the file cannot be created correctly.
2. Write a document.
The operation of writing files is advanced, otherwise it is meaningless to read empty files.
Since you are writing a binary file, you can write an integer value to it. Only write can be used to write binary characters.
Function. But the prototype of the write function is write(const char * ch, int size). The first parameter is char *
Type, so you need to convert int type to char * type. The transition here bothered me for several days, but I finally figured it out. The code is as follows. Internal temperature; file . write((char *)(& amp; temp)、sizeof(temp));
Step 3 read the document.
You can write files, and reading files will be much easier. Read function is required to read files. Its parameters and writing
Almost, read(const char * ch, int size). Reading content into int type variables also involves type conversion. It's like writing a document. int readIntfile . read((char(& amp; readInt)、sizeof(readInt)); In this way, the int value in the file is read into the int variable.
ReadInt won.
4. File pointer.
In the process of reading and writing files, it is often necessary to read files selectively. So you need to move the file pointer. This requires the use of seekg and seekp functions. There are two file pointers in the fstream class, one is a pointer to read a file.
One is a pointer to write a file. Tellg and tellp files are used to get the position of the pointer respectively. Similarly, two functions, seekg and seekp, are functions to move these two pointers. The parameters of these two functions are the same. First, explain several enumeration types:
IOS _ BASE::BEG- file start position IOS _ BASE::Cur- file current position IOS _ BASE::END- file end position Let's take seekg as an example to illustrate how to move the pointer: file. SEEKG(3)- The pointer moves to the position of the third character file.seekg(ios_base::beg). -pointer moved to file. seekg(IOs _ base::end)- pointer moved to file.seekg (-3, IOs _ base:: cur)-
The current position of pointer moves forward by three characters file.seekg (3, IOs _ base::cur)- the current position of pointer moves backward by three characters file.seekg (3, file. tellg())- the current position of pointer moves backward by three characters file. seekg(file. tellg()+3)- the current position of pointer moves backward by three characters.
Don't forget to close the file after you finish the operation.