C++ 更新二进制文件中的学生记录,同时删除以前的数据
我试图通过搜索他的卷号来更新学生记录,然后使用tellg和seekp移动到那个位置。但每当我试图覆盖时,它就会删除以前的记录C++ 更新二进制文件中的学生记录,同时删除以前的数据,c++,oop,file-io,C++,Oop,File Io,我试图通过搜索他的卷号来更新学生记录,然后使用tellg和seekp移动到那个位置。但每当我试图覆盖时,它就会删除以前的记录 int pos=0; Student obj; ifstream readfile("New.dat",ios::binary); cout<<"Enter Roll # of student"; cin>>a; while(!readfile.eof()) { readfile.read((char *) &am
int pos=0;
Student obj;
ifstream readfile("New.dat",ios::binary);
cout<<"Enter Roll # of student";
cin>>a;
while(!readfile.eof())
{
readfile.read((char *) &obj, sizeof(obj));
if(obj.getRoll()==a)
{
if(!readfile.eof())
{
pos=readfile.tellg();
obj.show();
cout<<"PLease Enter New Details of Student \n";
obj.get();
readfile.close();
ofstream file("New.dat",ios::binary);
file.seekp(pos);
file.write((char*)&obj,sizeof(obj));
cout<<"Student Record Has Been Updated \n";
countt=1;
}
}
}
file.close();
在读取对象之前,需要记录文件中的位置,这样才能回到同一位置再次写出对象
移动pos=readfile.tellg;在readfile.read行之前添加行。如果您提供一个可复制的示例,我们可能会更好地帮助您。使用二进制数据进行测试的一种方法是发布一个简单的程序,该程序将生成用于测试的初始二进制数据。我已经尝试过这种方法,但问题仍然存在,它删除了所有以前的记录,但更新的记录除外。