Update 12-9.cpp
This commit is contained in:
parent
9f58fef2ce
commit
4b0ba90862
@ -22,20 +22,20 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
ofstream out("D:/Employee.dat",ios::out); //定义随机输出文件
|
ofstream out("D:/Employee.dat",ios::out); //定义随机输出文件
|
||||||
Employee e1(1,"张三",23,2320);
|
Employee e1(1,"张三",23,2320);
|
||||||
Employee e2(2,"李四",32,3210);
|
Employee e2(2,"李四",32,3210);
|
||||||
Employee e3(3,"王五",34,2220);
|
Employee e3(3,"王五",34,2220);
|
||||||
Employee e4(4,"刘六",27,1220);
|
Employee e4(4,"刘六",27,1220);
|
||||||
out.write((char*)&e1,sizeof(e1)); //按e1,e2,e3,e4顺序写入文件
|
out.write((char*)&e1,sizeof(e1)); //按e1,e2,e3,e4顺序写入文件
|
||||||
out.write((char*)&e2,sizeof(e2));
|
out.write((char*)&e2,sizeof(e2));
|
||||||
out.write((char*)&e3,sizeof(e3));
|
out.write((char*)&e3,sizeof(e3));
|
||||||
out.write((char*)&e4,sizeof(e4));
|
out.write((char*)&e4,sizeof(e4));
|
||||||
|
|
||||||
//下面的代码将e3(即王五)的年龄改为40岁
|
//下面的代码将e3(即王五)的年龄改为40岁
|
||||||
Employee e5(3,"王五",40,2220);
|
Employee e5(3,"王五",40,2220);
|
||||||
out.seekp(3*sizeof(e1)); //指针定位到第3(起始为0)个数据块
|
out.seekp(2*sizeof(e1)); //指针定位到第3(起始为0)个数据块,这里写错了应该是2*sizeof(e1)
|
||||||
out.write((char*)&e5,sizeof(e5)); //将e5写到第3个数据块位置,覆盖e3
|
out.write((char*)&e5,sizeof(e5)); //将e5写到第3个数据块位置,覆盖e3
|
||||||
out.close(); //关闭文件
|
out.close(); //关闭文件
|
||||||
system("pause");
|
system("pause");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user