C++ 通过以下几个类支持文件的输入输出:
ofstream: 写操作(输出)的文件类 (由ostream引申而来)
ifstream: 读操作(输入)的文件类(由istream引申而来)
fstream: 可同时读写操作的文件类 (由iostream引申而来)
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
char buffer[10000];
ifstream examplefile("C:\\Users\\Administrator\\Downloads\\1.txt");
if (!examplefile.is_open())
{
cout << "Error opening file";
return 0;
}
while (!examplefile.eof())
{
examplefile.getline(buffer, 10000);
cout << buffer << endl;
}
return 0;
}
本文详细介绍了C++中文件输入输出的基本操作,包括使用ofstream、ifstream和fstream类进行文件的读写。通过实例演示了如何打开文件、读取文件内容并处理可能的错误。

被折叠的 条评论
为什么被折叠?



