如何将CIN和cout重定向到文件?我怎么才能重定向cin到in.txt和cout到out.txt?
3 回答
温温酱
TA贡献1752条经验 获得超4个赞
#include <cstdio>#include <iostream>using namespace std;int main(){ freopen("output.txt","w",stdout); cout<<"write in file"; return 0;}
ibeautiful
TA贡献1993条经验 获得超5个赞
#include <bits/stdc++.h>using namespace std;int main() { ifstream cin("input.txt"); ofstream cout("output.txt"); int a, b; cin >> a >> b; cout << a + b << endl;}
#include <bits/stdc++.h>using namespace std;void func() { int a, b; std::cin >> a >> b; std::cout << a + b << endl;}int main() { ifstream cin("input.txt"); ofstream cout("output.txt"); // optional performance optimizations ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin.rdbuf(cin.rdbuf()); std::cout.rdbuf(cout.rdbuf()); func();}
ios_base::sync_with_stdio
std::cin.rdbuf
#include <bits/stdc++.h>using std::endl;std::ifstream cin("input.txt");std::ofstream cout("output.txt");int a, b;void read() { cin >> a >> b;}void write() { cout << a + b << endl;}int main() { read(); write();}
std
using namespace std;
error: reference to 'cin' is ambiguous cin >> a >> b; ^note: candidates are: std::ifstream cin ifstream cin("input.txt"); ^ In file test.cpp std::istream std::cin extern istream cin; /// Linked to standard input ^
- 3 回答
- 0 关注
- 775 浏览
添加回答
举报
0/150
提交
取消