他过江 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 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题