我需要按顺序读取和写入同一个文件(没有 seekg 和 seekp),但由于某种原因,.write 不起作用!
这是我的课
class student
{
int id ;
char name[20] ;
int m1 , m2 , m3 ;
public:
student()
{
}
student(int idd , char*n , int mm1 , int mm2 , int mm3 )
{
id = idd ;
strcpy(name , n);
m1 = mm1 ;
m2 = mm2 ;
m3 = mm3 ;
}
void show()
{
cout<<"student id : "<<id<<endl<<"student name : "<<name<<endl<<"mark 1 : "<<m1<<endl<<"mrak 2 : "<<m2<<endl<<"mark 3 : "<<m3<<endl ;
}
void get()
{
cout<<"enter student id : " ;
cin>>id ;
cout<<"enter student name : " ;
cin.ignore() ;
cin.get(name , 20) ;
cout<<"enter student's mark 1 :" ;
cin>>m1 ;
cout<<"enter student's mark 2 :" ;
cin>>m2 ;
cout<<"enter student's mark 3 :" ;
cin>>m3 ;
}
};
这是我的主要功能:
int main()
{
fstream file("f://records.dat" , ios::out | ios::in |ios::binary ) ;
if(!file)
{
cout<<"Error !";
int z ;
cin>>z ;
return 4 ;
}
modify(file);
int x ;
cin>>x ;
return 0 ;
}
这是我的功能:
void modify(fstream &file)
{
int recnum ;
cout<<"enter the number of the record to be modified : " ;
cin>>recnum ;
file.seekg(0) ;
file.seekp(0);
student s1 ;
for(int i = 0 ; i<recnum-1 ; i++)
{
file.read((char *) &s1 , sizeof(s1)) ;
}
int x = file.tellp() ;
int y = file.tellg() ;
cout<<x<<endl<<y<<endl ;
student s2 ;
s2.get() ;
file.write((char *) &s2 , sizeof(student))
x = file.tellp() ;
y = file.tellg() ;
cout<<x<<endl<<y<<endl ;
file.flush() ;
file.seekg(0 , ios::beg);
if(file.eof())
{
cout<<"error !" ;
int x ;
cin>>x ;
}
while(file.read((char *) &s1 , sizeof(student)))
{
s1.show() ;
}
}
似乎方法修改中的写入功能不起作用所以请任何人帮助我?????