更新二进制文件中的学生记录,同时删除以前的数据
Updating student record in binary file, deletes previous data aswell
我试图通过搜索他的卷号来更新学生记录,然后使用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 *) &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
行之前。
相关文章:
- 从 Typedef 数据类型中删除常量
- 从输入中删除重复项,而不使用任何数据结构
- 删除使数据处于"错误状态"的默认构造函数的模式?
- 从自定义数据类型向量中删除重复元素
- 如何使用C++删除/更新txt文件中的特定数据
- 如何使用C++删除/更新txt文件中的特定数据?
- 如何从 txt 文件中删除数据/内容
- 如何根据偏移从文件中删除数据
- 如何在返回一次数据后从类实例中删除数据
- 凯撒密码删除数据
- Winsock:从套接字中删除数据
- 为什么我的矢量删除数据
- 设置/删除窗口子类时,正确传递/删除数据
- 双重删除数据不会崩溃
- 具有非常快速的内>数据查找和快速反向查找(搜索/插入/删除数据)的压缩字典?
- 当构造函数抛出异常时,如何删除数据成员的内存
- 从节点中删除数据
- 仅重定向子进程stdin,并从stdout和stderr中删除数据
- 删除数据文件中的条目
- 从链表中删除数据