源程序:
#include<iostream>
#include<fstream>
using namespace std;
class CStudent
{
public:
char id[11];
char name[21];
int score;
};
int main()
{
char ch;
cout << "确定需要向文件中追加新数据吗(Y/N)?";
cin >> ch;
if (ch == 'Y' || ch == 'y')
{
CStudent stu;
ofstream outFile("c:\tmp\student.dat", ios::app | ios::binary);
if (!outFile)
{
cout << "以追加方式打开文件失败" << endl;
return 0;
}
cout << "请输入:学号 姓名 成绩(以Ctrl+Z结束输入)
";
while (cin >> stu.id >> stu.name >> stu.score);
outFile.write((char*)&stu, sizeof(stu));
outFile.close();
}
system("pause");
return 0;
}
运行结果: