Binary file operations and how files are offset

1. Binary file operation

1. To perform binary file operations, you need to add ios::binary

2. The use of ofstream and ifstream

ofstream& write(const void* buf, int len);

Write len bytes of the content pointed to by the pointer buf to the file

ifstream& read(void* buf,int len);

Read len bytes from the file and store them in buf

3.bool eof()   if(!ifs.eof()){}

The secondary function is to judge whether the end of the file has been reached, if it reaches the end, it returns true, otherwise it returns false

Two, file offset

fstream actually inherits the fetch method and set method, and iostream is his parent class

File reading is get, file writing is put

tellg() Get the file read pointer

tellp() Get the file write pointer

seekg (offset, starting position) Set the file read pointer

seekp(offset, starting position) Set the file write pointer

The offset can be 1, 2 and other numbers

Start position: ios::cur current position ios::beg start position ios::end end position

The read and write offsets are changed by the write stream operation (ofs<<"xxx");

Change the read offset through the read stream operation (fs>>"xxx");

Change the read offset through the read stream operation (ofs>>"xxx");

seekg only changes the read offset

seekp only changes the write offset

Guess you like

Origin blog.csdn.net/m0_74889801/article/details/128839413