| 发表于:2007-01-05 14:11:528楼 得分:11 |
你试试吧 #include <iostream> #include <string> #include <fstream> using namespace std; typedef struct{ long long id; string name; int r1,r2,r3; }data,*pdata; int main() { data d; d.id=5050379001; d.name= "4k.grubby "; d.r1=85; d.r2=87; d.r3=83; data d0,d1; d0.id=5050379002; d0.name= "john "; d0.r1=d0.r2=d0.r3=90; d1.id=5050379003; d1.name= "tom "; d1.r1=d1.r2=d1.r3=87; ofstream file( "student.db ",ios::binary);//新建二进制文件 file.write((const char*)(&d),sizeof(data)); file.write((const char*)(&d0),sizeof(data)); file.write((const char*)(&d1),sizeof(data)); d1.name= "jerry ";//修改数据 file.seekp(2*sizeof(data));//移动到第三组数据处 file.write((const char*)(&d1),sizeof(data));//覆盖 file.close(); ifstream ifile( "student.db ",ios::binary); data d2; ifile.seekg(sizeof(data)*2); ifile.read((char*)(&d2),sizeof(data)); cout < < d2.name < <d2.id < < endl; ifile.close(); } | | |
|