首先跟你说 fstream 有两子类 一个ifstream 一个ofstream 一个输出 一个输入
直接建立fstream对象其实并不好 你运行代码可以看到没有1.txt文件 肯定失败呗
第二个是你文件里面写入了sd 和 df 就是firstName 和 lastName 但是却直接开始往整数数组里面读数据 肯定会把sd 和 df读到整数里面 这样也出现了错误
改了一下 你看看
#include
#include
using namespace std;
struct student
{
string firstname;
string lastname;
int score[10];
} stu[3];
int main()
{
ofstream p2("1.txt");
ifstream p1("1.txt");//分成了两个流
int n;
cin>>n;
for(int i=0; i{
cin>>stu[i].firstname;
cin>>stu[i].lastname;
for(int j=0; j<10; j++)
cin>>stu[i].score[j];
}
for(int i=0; i{
p2<p2< for(int j=0; j<10; j++)
p2<p2< }
string str;//这里加了个字符串
p1>>str>>str;//用来抵消 开始输入的两个字符串
int a[10][10]={0};
for(int i=0; ifor(int j=0; j<10; j++)
p1>>a[i][j];
for(int i=0; i{
for(int j=0; j<10; j++)
cout< cout<}
p1.close();
p2.close();
}
#include
#include
#include
using namespace std;
struct student
{
string firstname;
string lastname;
int score[10];
} stu[3];
int main()
{
fstream p1("1.txt");//确认已存在1.txt
int n;
cin>>n;
for(int i=0; i{
cin>>stu[i].firstname;
cin>>stu[i].lastname;
for(int j=0; j<10; j++)
cin>>stu[i].score[j];
}
for(int i=0; i{
p1<p1< for(int j=0; j<10; j++)
p1<p1< }
p1.seekp(0);//文件定位到开头
string str1, str2;
int a[10][10]={0};
for(int i=0; i{
p1>>str1>>str2;//把两个字符串(名字)读出
for(int j=0; j<10; j++)
p1>>a[i][j];
}
for(int i=0; i{
for(int j=0; j<10; j++)
cout< cout<}
p1.close();
}