先删除function1.cpp110行的write()函数调用,然后将下面复制覆盖function5.cpp
#include "menu_all.h"
#include "reach5.h"
string idcard;
string name;
string sex;
int age;
double averagesteps;
status reach5(user &users) {
while(1){
menu5();
cout << "请选择:";
int x=-1;
cin >> x;
switch (x) {
case 0: system("cls");return 1;
case 1:read(users); break;
case 2:writeall(users);break;
default:cout << "Input Error,Please Rnew Input"<<endl; break;
}
system("pause");
system("cls");
}
return 1;
}
status read(user &users) {//读取用户信息
int n,j=0;
people t1;
fstream f("C:\\Users\\a1474\\OneDrive\\桌面\\新建项目\\stu.txt",ios::in);
if (!f) //打开失败
{
cout << "文件打开失败!" << endl;
return 1;
}
else {
cout << "文件打开成功" << endl;
}
while (f >> t1.idcard>>t1.name >> t1.sex >> t1.age)
{
n = 0;
while (f >> t1.step[n])
{
n++;
if (n == 7)
break;
}
f >> t1.averagesteps;
f >> t1.day;
users.elem[j++]=t1;
users.length++;
}
f.close();
for (int i = 0; i <users.length; i++)
{
cout << setw(16) << users.elem[i].idcard;
cout << left << setw(8) << users.elem[i].name;
cout << left << setw(8) << users.elem[i].sex;
cout << left << setw(8) << users.elem[i].age;
for (int j = 0; j < 7; j++)
{
cout << left << setw(8) << users.elem[i].step[j];
}
cout << left << setw(16) << users.elem[i].averagesteps;
//cout << left << setw(8) << users.elem[i].day;
cout << endl;
}
return 1;
}
status writeall(user &users){
fstream f;
f.open("C:\\Users\\a1474\\OneDrive\\桌面\\新建项目\\stu4.txt",ios::out);
if(!f){
cout<<"文件打开失败!"<<endl; return 1;
}
for( int i=0; i<users.length; i++) {
f<<left<<setw(16)<<users.elem[i].idcard;
f<<left<<setw(8)<<users.elem[i].name;
f<<left<<setw(8)<<users.elem[i].sex;
f<<left<<setw(8)<<users.elem[i].age;
for(int j=0;j<7;j++)
{
f<<left<<setw(8)<<users.elem[i].step[j];
}
f<<left<<setw(16)<<users.elem[i].averagesteps;
f<<endl;
}
f.close();
cout<<"文件保存成功!"<<endl;
return 1;
}