自考新教材-p308

源程序:

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char id[11], name[21];
int score;
ofstream outFile;
outFile.open("c:\\tmp\\score.txt", ios::out); //以写方式打开文本文件
if (!outFile) //条件成立,则说明文件打开出错
{
cout << "创建文件失败" << endl;
return 0;
}
cout << "请输入:学号 姓名 成绩(以Ctrl+Z结束输入)\n";
while (cin >> id >> name >> score)
outFile << id << " " << name << " " << score << endl; //向流中插入数据
outFile.close();
return 0;
}

运行结果:

运行时输入以下数据,在c:\tmp\score.txt中查看结果如下:

猜你喜欢

转载自www.cnblogs.com/duanqibo/p/12263331.html
今日推荐