#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>

int main()
{
using namespace std;

ifstream inStream;
ofstream outStream;

inStream.open("infile.dat");
if (inStream.fail()) {
cout << "打开文件 infile.dat 失败" << endl;
exit(1);
}

outStream.open("outfile.dat");
if (outStream.fail()) {
cout << "打开文件 outfile.dat 失败" << endl;
exit(1);
}

int first, second, thrid;
inStream >> first >> second >> thrid;
outStream << first << endl << second << endl << thrid << endl;

inStream.close();
outStream.close();

return 0;
}

输出:

C++ 简单的文件流测试_C/C++

infile.dat

C++ 简单的文件流测试_打开文件_02

outfile.dat

C++ 简单的文件流测试_#include_03