第四节 · 摄取与排放数据 · 文件输入输出
类
说明
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ofstream outFile("data.txt"); // 创建/打开文件
if (!outFile)
{
cerr << "无法打开文件!" << endl;
return 1;
}
// 写入数据(与 cout 用法相同)
outFile << "Hello, File!" << endl;
outFile << 42 << endl;
outFile << 3.14 << endl;
outFile.close(); // 关闭文件
cout << "数据已写入文件" << endl;
return 0;
}模式
说明
习题
Last updated