C++文件重定向
方法一
命令行使用 < 和 >
| func.exe < infile.txt > outfile.txt
|
方法二
C++实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include <iostream> #include <ostream> #include <fstream> using namespace std; int main() { ifstream fin("input.txt"); ofstream fout("output.txt");
streambuf *cinbackup; streambuf *coutbackup;
coutbackup= cout.rdbuf(fout.rdbuf()); cinbackup= cin.rdbuf(fin.rdbuf()); cout<<"Hello world"<<endl;
char line[100]; cin>>line; cout<<line<<endl; cin.rdbuf(cinbackup); cout.rdbuf(coutbackup); return 0; }
|