C++ Move and Get File Read/Write Pointers (seekp, seekg, tellg, tellp)

C++ Move and Get File Read/Write Pointers (seekp, seekg, tellg, tellp)When reading and writing files, sometimes you want to jump directly to a certain position in the file to start reading or writing. This requires first setting the file’s read/write pointer to that position before performing read or write operations.

  • The ifstream class and fstream class have the seekg member function, which can set the position of the file read pointer;
  • The ofstream class and fstream class have the seekp member function, which can set the position of the file write pointer.

The so-called “position” refers to the number of bytes from the beginning of the file. The position at the beginning of the file is 0. The prototypes of these two functions are as follows:ostream & seekp (int offset, int mode);istream & seekg (int offset, int mode);mode represents the setting mode of the file read/write pointer, which has the following three options:

  • ios::beg: This sets the file read pointer (or write pointer) to the offset bytes from the beginning of the file. An offset of 0 means the start of the file. In this case, the offset can only be a non-negative number.
  • ios::cur: In this case, if the offset is negative, it means moving the read pointer (or write pointer) towards the beginning of the file by offset bytes; if positive, it means moving the read pointer (or write pointer) towards the end of the file by offset bytes; if 0, it means no movement.
  • ios::end: This sets the file read pointer (or write pointer) to |offset| bytes from the end of the file. In this case, the offset can only be 0 or negative.

Additionally, we can also obtain the current position of the read/write pointer:

  • The ifstream class and fstream class also have the tellg member function, which can return the position of the file read pointer;
  • The ofstream class and fstream class also have the tellp member function, which can return the position of the file write pointer.

The prototypes of these two member functions are as follows:int tellg();int tellp();To get the file length, you can use the seekg function to set the file read pointer to the end of the file, and then use the tellg function to get the position of the file read pointer, which will be the length of the file.Example: Assume the student record file students.dat is sorted by name, write a program to find the student record with the name Jack in the students.dat file using binary search, and change their age to 20 (assuming the file is too large to read into memory all at once). The program is as follows:

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
class CStudent
{
public:
char szName[20];
int age;
};
int main()
{
CStudent s;
fstream ioFile("students.dat", ios::in|ios::out);//Open in read/write mode
if(!ioFile) {
cout << "error" ;
return 0;
}
ioFile.seekg(0,ios::end); //Set read pointer to the end of the file,
//So that tellg can be used to get the file length
int L = 0,R; // L is the index of the first record in the binary search range
// R is the index of the last record in the binary search range
R = ioFile.tellg() / sizeof(CStudent) - 1;
// The last record index of the first search range is: total number of records - 1
do {
int mid = (L + R)/2; // Compare the record in the middle of the search range with the name to be searched
ioFile.seekg(mid *sizeof(CStudent),ios::beg); //Set to the middle record
ioFile.read((char *)&s, sizeof(s));
int tmp = strcmp( s.szName,"Jack");
if(tmp == 0) { //Found
s.age = 20;
ioFile.seekp(mid*sizeof(CStudent),ios::beg);
ioFile.write((char*)&s, sizeof(s));
break;
}
else if (tmp > 0) //Continue searching in the first half
R = mid - 1 ;
else  //Continue searching in the second half
L = mid + 1;
}while(L <= R);
ioFile.close();
return 0;
}

The editor has compiled a set of C Language learning materials, if you need them, you can private message @C Language Learning Alliance and reply to receive materials, everyone is welcome to follow, I will share related technical blogs in a timely manner, your attention and likes are very important to me, thank you all for clicking and following~*Disclaimer:This article is compiled from the internet, the copyright belongs to the original author. If there is any error in the source information or infringement of rights, please contact us for deletion or authorization matters.

Leave a Comment