C++多文件输入输出

Posted on 2026-10-21 20:57  暮光落阳  阅读(9)  评论(0)    收藏  举报

C++文件读入

在调试过程中,可能需要同时从多个文件读入,但 freopen 只会从最后指定的文件读入。

所以,假定从 test1.in 和 test2.in 读入,可以:

#include <ifstream>
using namespace std;

ifstream in1("test1.in");
ifstream in2("test2.in");

读入可以使用类似 cin 的写法:

int a,b;
in1 >> a;
in2 >> b;

输出也类似:

#include <ofstream>
using namespace std;

ofstream ot1("test1.out");
ofstream ot2("test2.out");

int main(){  

    ot1 << 1;
    ot2 << 2;
    
    return 0;
}

这与终端的输入输出是互不影响的。与 freopen 混用,读入时二者互不影响,输出时,结果很混乱。


虽然很简略也很不严谨,但是对拍已经够用了吧。