C/C++教程

C++ 简单的文件流测试

本文主要是介绍C++ 简单的文件流测试,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#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;
}

输出:

infile.dat

outfile.dat

这篇关于C++ 简单的文件流测试的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!