为什么本人从磁盘读入数据再将数据写入磁盘,打开txt文件还是没写入?然后把从磁盘读入数据的代码删了就能写入了

C++语言 码拜 8年前 (2016-04-26) 655次浏览
pragma once
#include”emp.h”
void Add(Employee &t)
{
Employee a;
fstream iofile(“emp.txt”, ios::in|ios::out);
if (!iofile)
{
cerr << “open error” << endl;
system(“pause”);
exit(-1);
}
for (int i = 0;i < 50;++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();
}
cout << “输入num,name,sex,age,wage” << endl;
cin >> t.num>>t.name>>t.sex>>t.age>>t.wage; //输入一组数据到内存
t.display();
iofile << t.num<<” “<<t.name<<” “<<t.sex<<” “<<t.age<<” “<<t.wage<<endl;  //将数据输出到磁盘
iofile.close();
}
解决方案

40

原因是写文件是行缓冲,必须flush 下,才会真正的写进去。

喜欢 (0)
[1034331897@qq.com]
分享 (0)