【发布时间】:2018-10-13 11:51:44
【问题描述】:
我编写了一个程序来使用 c++ 文件流删除存储在二进制文件中的类对象。在此过程中,我必须将所有对象从一个文件(example.dat)复制到另一个文件(temp.dat)。
我有一个 静态变量 作为类的一部分,我希望将它与对象一起复制到其中。 但静态变量并没有复制到 temp.dat,它在 temp.dat 中的值为 0,因为静态变量不是任何对象的一部分。
这是我使用的函数和类定义
{ //the problem is in this function
cout<<"\nSno of record to delete: ";
int del;
cin>>del;
fstream o;
o.open("temp.dat",ios::out|ios::in|ios::binary);
if(!f)
{
cout<<"File not Found";
exit(0);
}
else
{f.seekp(0);
f.read((char*)&dats, sizeof(dats));
while(!f.eof())
{
if(dats.sno!=del)
{
o.write((char*)&dats, sizeof(dats));
}
f.read((char*)&dats, sizeof(dats));
}
}
o.close();
f.close();
remove("date.dat");
rename("temp.dat", "date.dat");
return 0; }
类定义
class date{
int d,m,y;
int k;
char dday[10];
char monthn[10];
char name[50];
public:
int sno;
int odd ();
void getdata();
int fsno();
void display();
static int ID; //static variable
}
请提出解决此问题的方法
【问题讨论】:
标签: c++ class file-io static binaryfiles