c++ fstream

来源:百度知道 编辑:UC知道 时间:2024/07/05 05:52:17
我对fstream不是很了解,现在要做一个程序,就是txt文件和c++之间的读取和写入,如:“学号:0001 姓名:张三 成绩:100”这样的数据用c++写入txt怎么做到?
这样的数据在txt文件中又怎么读取?举例说明,谢谢!
就写一个程序,把:姓名,学号,成绩等实现写入,读取和修改就可以了。

#include<iostream>
#include<fstream>
using namespace std;
struct student
{
char number[10];
char name[10];
float score;
};
int main()
{
student a[3]={ //create the array.
{"0001","jim",89.0},
{"0002","tom",97.0},
{"0003","great",76.0}
};
ofstream fout("student.txt");
if(!fout)
{
cout<<"open failed!"<<endl;
exit(1);
}
for(int i=0;i<3;i++)
{
fout<<a[i].number<<" "<<a[i].name<<" "<<a[i].score<<endl;//write the file
}
fout.close();
ifstream fin("student.txt");
if(!fin)
{
cout<<"open failed!"<<endl;
exit(1);
}
student s[3];
i=0;
while(fin>>s[i].number>>s[i].name>>s[i].