#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class student
{
public:
int getClassid()
{
return classID;
}
void setClassid(int cl_id)
{
classID = cl_id;
}
int getid()
{
return id;
}
void setid(int s_id)
{
id = s_id;
}
string getName()
{
return name;
}
void setName(string _name)
{
name = _name;
}
private:
int classID;
int id;
string name;
};
int main()
{
student st[3];
int classid, id;
string name;
cout << "需要输入三个学生的信息" << endl;
for (int i = 0; i < 3; i++)
{
cout << "输入第" << i + 1 << "个学生所在班级:" << endl;
cin >> classid;
cout << "输入第" << i + 1 << "个学生学号:" << endl;
cin >> id;
cout << "输入第" << i + 1 << "个学生姓名:" << endl;
cin >> name;
st[i].setClassid(classid);
st[i].setid(id);
st[i].setName(name);
}
ofstream os;
os.open("d:\\a.txt");
if (os)
{
os << "班级 " << "学号 " << "姓名" << endl;
for (int i = 0; i < 3; i++)
{
os << st[i].getClassid() <<" " << st[i].getid() << " " << st[i].getName();
if (i <= 1)os << endl;
}
os.close();
cout << "写入数据成功!" << endl;
}
else
{
cout << "打开文件错误!" << endl;
}
char line[1024];
ifstream is;
is.open("d:\\a.txt");
int i = 0;
if(is)
{
is.getline(line,1024);
while (!is.eof() && i < 3)
{
is >> classid >> id >> name;
st[i].setClassid(classid);
st[i].setid(id);
st[i].setName(name);
i++;
}
is.close();
cout << "读入数据成功!" << endl;
}
else
{
cout << "打开文件错误!" << endl;
}
cout << "班级 " << "学号 " << "姓名" << endl;
for (int i = 0; i < 3; i++)
{
cout << st[i].getClassid() << " " << st[i].getid() << " " << st[i].getName() << endl;
}
system("pause");
return 0;
}
老师版本,终极的终极版