冷知识,文件读写不一定要用freopen

比如说有一道文件读写的a+b

#include <bits/stdc++.h>
using namespace std;
int a, b;
int main() {
    freopen("aplusb.in", "r", stdin);
    freopen("aplusb.out", "w", stdout);
    cin >> a >> b;
    cout << a + b;
    return 0;
}

这么写也能过

#include <bits/stdc++.h>
using namespace std;
int a, b;
int main() {
	ifstream fin("aplusb.in");
	ofstream fout("aplusb.out");
	fin >> a >> b;
	fout << a + b;
    return 0;
}

或者这样也行

#include <bits/stdc++.h>
using namespace std;
int a, b;
int main() {
	ifstream ccf("aplusb.in");
	ofstream gesp("aplusb.out");
	ccf >> a >> b;
	gesp << a + b;
    return 0;
}

总结

文件读写只要是文件读写就行,无所谓方式,但是由于freopen更加方便,所以没人用ifstreamofstream

听懂掌声!

6 个赞

内容我个人感觉没什么实用性,就纯冷知识,但看你这个头像就必须点赞了

2 个赞

愛道法.瞎討論舊像

此话题已在最后回复的 15 天后被自动关闭。不再允许新回复。