他过江 2016-05-22 16:16 采纳率: 0%
浏览 1043

我有三个这样的头文件,刚开始只有一个头文件的时候打开文件没问题,现在都打开失败怎么办?

#pragma once
void Add()
{
Employee a[MAXK],t;
fstream iofile("emp.txt", ios::in|ios::out);
if (!iofile)
{
cerr << "open error" << endl;
system("pause");
exit(-1);
}
for (int i = 0;i < MAXK;++i)
{
iofile >> a[i].num>>a[i].name>>a[i].sex>>a[i].age>>a[i].wage ;//从磁盘读入数据
if (a[i].num == -1)
break;
a[i].display();
}
iofile.close();
iofile.clear();
cout << "输入num,name,sex,age,wage" << endl;
cin >> t.num>>t.name>>t.sex>>t.age>>t.wage; //输入一组数据到内存
t.display();
iofile.open("emp.txt",ios::app);

iofile << t.num<<" "< iofile.close();
iofile.clear();
}
void Delete()
{
char se;
int j=-1, Num;
string Name;
Employee a[100];
fstream iofile("emp.txt", ios::in | ios::out);
if (!iofile)
{
cerr system("pause");
exit(-1);
}
for (int i = 0;i {
iofile >> a[i].num >> a[i].name >> a[i].sex >> a[i].age >> a[i].wage;//从磁盘读入数据
if (a[i].num == -1)
break;
a[i].display();
}
iofile.close();
iofile.clear();
cout << "工号查询请按1,名字查询请按2:" << endl;
cin >> se;
switch (se)
{
case '1':
cout << "输入工号:" << endl;
cin >> Num;
for (int i = 0;i < MAXK&&a[i].num != -1;++i)
if (Num == a[i].num)
j = i;break;//j保存查找到的员工索引
break;//退出case
case '2':
cout << "输入姓名:" << endl;
cin >> Name;
for (int i = 0;i < MAXK&&a[i].num != -1;i++)
if (Name == a[i].name)
j = i;break;
break;//退出case
default:cout << "输入错误" << endl;
}
if (j==-1)
{
cout << "没有找到该员工" << endl;
return;
}
iofile.open("emp.txt", ios::out);
for(int i=j;i < MAXK && a[i].num != -1;++i)
a[i] = a[i + 1];
for (int i = 0;i < MAXK && a[i].num != -1;++i)
iofile << a[i].num << " " << a[i].name << " " << a[i].sex << " " << a[i].age << " " << a[i].wage << endl;
iofile.close();
iofile.clear();
cout << "删除完成" << endl;
}
void Search()
{
char se;
int j = -1, Num;
string Name;
Employee a[100];
fstream iofile("emp.txt", ios::in | ios::out);
if (!iofile)
{
cerr << "open error" << endl;
system("pause");
exit(-1);
}
for (int i = 0;i < MAXK;++i)
{
iofile >> a[i].num >> a[i].name >> a[i].sex >> a[i].age >> a[i].wage;//从磁盘读入数据
if (a[i].num == -1)
break;
a[i].display();
}
iofile.close();
iofile.clear();
cout << "工号查询请按1,名字查询请按2:" << endl;
cin >> se;
switch (se)
{
case '1':
cout << "输入工号:" << endl;
cin >> Num;
for (int i = 0;i < MAXK&&a[i].num != -1;++i)
if (Num == a[i].num)
j = i;break;//j保存查找到的员工索引
break;//退出case
case '2':
cout << "输入姓名:" << endl;
cin >> Name;
for (int i = 0;i < MAXK&&a[i].num != -1;i++)
if (Name == a[i].name)
j = i;break;
break;//退出case
default:cout << "输入错误" << endl;
}
if (j == -1)
{
cout << "没有找到该员工" << endl;
return;
}
cout << "该员工的信息:" << endl;
a[j].display();
}
void Modify()
{
char se;
int j = -1, Num;
string Name;
Employee a[100];
fstream iofile("emp.txt", ios::in | ios::out);
if (!iofile)
{
cerr << "open error" << endl;
system("pause");
exit(-1);
}
for (int i = 0;i < MAXK;++i)
{
iofile >> a[i].num >> a[i].name >> a[i].sex >> a[i].age >> a[i].wage;//从磁盘读入数据
if (a[i].num == -1)
break;
a[i].display();
}
iofile.close();
iofile.clear();
cout << "工号查询请按1,名字查询请按2:" << endl;
cin >> se;
switch (se)
{
case '1':
cout << "输入工号:" << endl;
cin >> Num;
for (int i = 0;i < MAXK&&a[i].num != -1;++i)
if (Num == a[i].num)
j = i;break;//j保存查找到的员工索引
break;//退出case
case '2':
cout << "输入姓名:" << endl;
cin >> Name;
for (int i = 0;i < MAXK&&a[i].num != -1;i++)
if (Name == a[i].name)
j = i;break;
break;//退出case
default:cout << "输入错误" << endl;
}
if (j == -1)
{
cout << "没有找到该员工" << endl;
return;
}
cout << "该员工的信息:" << endl;
a[j].display();
cout << "改工号输入1:" << endl;
cout << "改名字输入2:" << endl;
cout << "改性别输入3:" << endl;
cout << "改年龄输入4:" << endl;
cout << "改工资输入5:" << endl;
char cho;
cin >> cho;
cout << "输入修改数据:" << endl;
switch (cho)
{
case'1':cin >> a[j].num;break;
case'2':cin >> a[j].name;break;
case'3':cin >> a[j].sex;break;
case'4':cin >> a[j].age;break;
case'5':cin >> a[j].wage;break;
default:cout << "enter error" << endl;
}
cout << "修改后的员工信息:" << endl;
a[j].display();
iofile.open("emp.txt", ios::out);
for (int i = 0;i < MAXK && a[i].num != -1;++i)
iofile << a[i].num << " " << a[i].name << " " << a[i].sex << " " << a[i].age << " " << a[i].wage << endl;
iofile.close();
iofile.clear();
}

  • 写回答

1条回答 默认 最新

  • threenewbee 2016-05-22 21:22
    关注

    通过fstream写的文件是二进制的,不是文本文件。记事本里显示为乱码。如果你用记事本修改过,再打开就会出错。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀