菜鸟请求帮忙,大虾救命
大虾,这个是我在测试输入输出流时所写的代码,功能是输入学生的成绩并保存,经过编译已经是没有任何语法问题的了,但是问题就出在我把编译窗口关了以后,想直接查看原本文件中的记录时,却没办法看了,你请问该怎么办呀?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class student
{
private:
long num;
string name;
float score;
public:
void setnum()
{
cout << "请输入学生的学号:" << endl;
cin >> num;
}
void setname()
{
cout << "请输入学生的姓名:" << endl;
cin >> name;
}
void setscore()
{
cout << "请输入学生的成绩:" << endl;
cin >> score;
}
long getnum()
{
return num;
}
string getname()
{
return name;
}
float getscore()
{
return score;
}
};
int main()
{
ofstream outfile("student.dat", ios::binary || ios::ate);
if (!outfile)
{
cout << "File student.dat cannot be opened." << endl;
return 0;
}
student stud[100];
char ch;
int i = 0;
while (1)
{
cout << "\n 你想输入更多的记录吗?Y/N " << endl;
cin >> ch;
if (ch == 'n' || ch == 'N')
break;
i++;
stud[i].setnum();
stud[i].setname();
stud[i].setscore();
outfile.write((char*)&stud[i], sizeof(student));
}
outfile.close();
cout << "************************输入结束***************************" << endl;
cout << "\n 你想查看文件内容吗?y/n " << endl;
cin >> ch;
if (ch == 'y' || ch == 'Y')
{
ifstream infile("student.dat", ios::binary);
if (!infile)
{
cout << "file student.dat cannot be opened." << endl;
return 0;
}
cout << "学号" << "\t学号" << "\t姓名" << "\t成绩" << endl;
i = 1;
infile.read((char*)&stud[i], sizeof(student));
while (infile)
{
cout << stud[i].getnum() << "\t";
cout << stud[i].getname() << "\t";
cout << stud[i].getscore() << "\t" << endl;
i++;
infile.read((char*)&stud[i], sizeof(student));
}
infile.close();
}
return 0;
}