string filename; ofstream outfile("D:\fname.txt"); // 打开文件写入 if (!outfile) { cout << "文件打开失败!" << endl; return; } for (auto p = i.v.begin(); p != i.v.end(); ++p) { outfile << p->num << " " << p->name << " " << p->sex << " " << p->score1 << " " << p->score2 << " " << p->score3 << endl; } outfile.close(); // 关闭文件完善上述代码使其实现保存到fname。txt上
时间: 2024-04-07 16:31:17 浏览: 144
outfile C++写入文件
string filename;
cout << "请输入文件名:";
cin >> filename;
filename = "D:\\" + filename + ".txt"; // 拼接完整的文件路径
ofstream outfile(filename);
if (!outfile) {
cout << "文件打开失败!" << endl;
return;
}
for (auto p = i.v.begin(); p != i.v.end(); ++p) {
outfile << p->num << " " << p->name << " " << p->sex << " " << p->score1 << " " << p->score2 << " " << p->score3 << endl;
}
outfile.close();
cout << "保存成功!" << endl;
阅读全文