一个C++中关于文件的问题!那位学哥学姐能帮我解决啊?

bcypxl 2011-11-17 11:30:27
谁能帮我解决下面的问题,辛辛苦苦写出来的,功能上有缺陷,找不出错在那里,很纠结。
对于大家的帮助我会很感激的。




同学通信录程序,编写一个程序,将下面的同学信息存储到文件中。
name:具有21个字符空间的数组
age:一个整形变量
address:具有51个空间的字符数组
phone:具有14个空间的字符数组
email:具有51个空间的字符数组
该程序具有一个菜单,便于用户完成如下操作:
1)向文件中增加记录。
2)显示文件中的所有记录。
3)修改任意一个记录。
4)按照姓名查找一个同学的记录。
5)删除某个同学的记录。
输入有效性检验:输入的年龄不能为负,也不能大于200.

#include"iostream"
using namespace std;
#include"fstream"
#include"iomanip"

struct Info
{
char name[21];
int age;
char address[51];
char phone[14];
char email[51];
};

int i=0; //用来记录读指针到底几个位置了

void AddFile(fstream &);
void ShowFile(fstream &);
void EditFile(fstream &);
void SearchFile(fstream &);
void DeleteFile(fstream &);

int main()
{
fstream people("Info.dat",ios::in|ios::out|ios::binary);
int choice;

if(people.fail())
{
cout << "文件打开失败!\n";
exit(0);
}
while(true)
{
cout << "\n\t1: Add 2: Show 3: Edit 4: Search 5: Delete 6: Exit\n";
cin >> choice;
switch(choice)
{
case 1:
AddFile(people);
break;
case 2:
ShowFile(people);
break;
case 3:
EditFile(people);
break;
case 4:
SearchFile(people);
break;
case 5:
DeleteFile(people);
break;
case 6:
exit(0);
}
}
return 0;
}
//向文件中增加记录
void AddFile(fstream &file) //问:第一次添加时可以,但第二次添加时加不上啊?
{
Info person;

cout << "请输入下面的新信息:\n";
cout << "姓名:";
cin >> person.name;
cout << "年龄:";
cin >> person.age;
cin.ignore();
cout << "地址:";
cin.getline(person.address,51);
cout << "电话:";
cin.getline(person.phone,14);
cout << "E-mail:";
cin.getline(person.email,51);
file.seekp(0L,ios::end);//每次输入时,读指针都在文件尾,前面的数据不会被冲掉
file.write((char *)&person,sizeof(person));
file.flush();
}
//显示文件中的所有记录
void ShowFile(fstream &file)
{
Info person;

file.clear();
file.seekg(0L,ios::beg);
while(!file.eof())
{
file.read((char *)&person,sizeof(person));
if(file.fail())
break;
cout << "姓名:" << person.name;
cout << setw(20) << "年龄:" << person.age;
cout <<setw(20) << "地址:" << person.address << endl;
cout << "电话:" << person.phone;
cout << setw(21) << "E-mail" << person.email << endl;
cout << "**********************************************************";
cout << endl;
}
}
//修改任意一个记录
void EditFile(fstream &file)
{
Info person;


cout << "输入要修给信息的同学姓名并显示原信息" << endl;
cout << "**********************************************************" << endl;
i = 0;
SearchFile(file);

cout << "下面请输入新的信息:" << endl;
cout << "姓名:";
cin >> person.name;
cout << "年龄:";
cin >> person.age;
cin.ignore();
cout << "地址:";
cin.getline(person.address,51);
cout << "电话:";
cin.getline(person.phone,14);
cout << "E-mail:";
cin.getline(person.email,51);

file.seekp(i * sizeof(person),ios::beg); //使读指针直接指向要修改记录的地方
file.write((char *)&person,sizeof(person));
file.flush();

}
//按照姓名查找一个记录
void SearchFile(fstream &file) //可以正常的查找信息
{
Info person;
char compt[21];


file.clear();
cout << "请输入要查找的同学姓名:";
cin >> compt;
cin.ignore();
while(!file.eof())
{
file.seekg(i * sizeof(person),ios::beg);
file.read((char *)&person,sizeof(person));
if(file.fail())
break;
if(strcmp(compt,person.name) == 0)
{
cout << "姓名:" << person.name;
cout << setw(20) << "年龄:" << person.age;
cout << setw(20) << "地址:" << person.address << endl;
cout << "电话:" << person.phone;
cout << setw(21) << "E-mail" << person.email << endl;
cout << "**********************************************************";
cout << endl;
break;
}
i++;
}
}
//删除某个同学的记录
void DeleteFile(fstream &file) //问:想删除的记录删不掉啊?
{
Info person;
char choice;
int j;

cout << "请输入要删除信息同学的名字并显示其信息" << endl;
cout << "**********************************************************" << endl;
i = 0;
SearchFile(file);
j = i;

cout << "你确定要删除该同学信息吗!(输入Y或N)" << endl;
cin >> choice;
if(toupper(choice) == 'Y')
{
while(!file.eof())
{
file.seekg((j+1) * sizeof(person),ios::beg); //使读指针指向要修改数据的下一个位置
file.read((char *)&person,sizeof(person)); //读取数据
if(file.fail())
break;
file.seekp(j * sizeof(person),ios::beg); //使写指针直接指向要修改记录的地方
file.write((char *)&person,sizeof(person));
file.flush();
j ++;
}
}
else
cout << "信息未删除!" << endl;
}
...全文
126 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2011-11-17
  • 打赏
  • 举报
回复
摒弃cin、cout;
使用scanf、printf!
zhaghi 2011-11-17
  • 打赏
  • 举报
回复
有什么缺陷,你要说出来才能帮你解决,你不会叫大家帮你找bug吧
healer_kx 2011-11-17
  • 打赏
  • 举报
回复
写的不错,继续DEBUG。
langyano1 2011-11-17
  • 打赏
  • 举报
回复
同学,说出遇到的问题吧,听一楼的,没有太大工夫每行都看,并且你报了错,NB的人通常大概就知道原因,定位得到那几行!对你有好处的
LKing_2011 2011-11-17
  • 打赏
  • 举报
回复
标记一下,有空的时候来看看。呵呵
Richard-Benjamin 2011-11-17
  • 打赏
  • 举报
回复
我也在学文件输入输出流,我写的也有问题,程序结束异常,现在也在纠结啊!网上查了很多对象序列化的问题,不过还是不懂啊!纠结!
qq120848369 2011-11-17
  • 打赏
  • 举报
回复
很不错,继续看书巩固知识.

64,635

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧