编写程序,从键盘输入3个学生的数据,将它们存入文件student;然后再从文件中读出数据,现实在屏幕上。

[复制链接]
查看11 | 回复0 | 2006-11-2 16:33:15 | 显示全部楼层 |阅读模式
希望下面的代码能够给你启发,你可以试着在代码中添加查询功能等#include#include#include#include#include#define N 20struct Student{ char name[N];};class Tel{public: void adduser(); void displayalluser();};class Application{private: Tel t;
int choice;public: void printmenu() {system("cls");cout>choice;
switch(choice) { case 1:
t.adduser();
break; case 2:
t.displayalluser();
break; case 3:
exit(1); default:
cout<<"输入错误"<<endl;
break; }} }};//添加void Tel::adduser(){ ofstream f; Student s; system("cls"); cout<<"添加新学生资料:"<<endl; f.open("aa",ios::app|ios::binary); cout<<"姓名:"; f.write((char *)&s,sizeof(Student)); f.close();}//输出资料void Tel::displayalluser(){ system("cls"); ifstream f;Student s; f.open("aa",ios::in|ios::binary); //保存到文件aawhile(f.read((char *)&s,sizeof(Student))) {cout<<s.name<<"\n";} f.close();}void main(){ Application theapp; theapp.run();}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行