- 星座
- 摩羯座
- 性别
- 男
- 积分
- 105
- 积分
- 157
- 精华
- 0
- 阅读权限
- 20
- 注册时间
- 2013-6-5
- 最后登录
- 2014-3-24
- 帖子
- 6
- 星座
- 摩羯座
- 性别
- 男
|
admin 发表于 2013-12-16 17:01
谢谢,我也写了个- #include<iostream>
- #include<fstream>
- using namespace std;
- struct People
- {
- int num;
- char name[18];
- };
- int main()
- {
- People peo[30];
- int num;
- char name[18];
- int n;
- cout<<"plese input the whole number"<<endl;
- cin>>n;
- for(int i=0;i<n;i++)
- {
- cout<<"please input the num of people:"<<i+1<<endl;
- cin>>peo[i].num;
- cout<<"please input the name of people:"<<i+1<<endl;
- cin>>peo[i].name;
- }
- fstream f("peo.dat",ios::out|ios::binary);
- f.close();
- f.open("peo.dat",ios::in|ios::out|ios::binary);
- if(f.fail())
- {
- cout<<"error for open"<<endl;
- exit(1);
- }
- f.write((char*)&peo[0],sizeof(People)*n);
- People p;
- f.seekp(0);
- f.read((char*)&p,sizeof(People));
- while(!f.eof())
- {
- cout<<endl<<p.num<<endl<<p.name<<endl<<endl;
- f.read((char*)&p,sizeof(People));
- }
- f.close();
- return 0;
- }
复制代码 |
|