Naw*_*waz 197
以下是您想要做的工作示例.阅读注释以了解代码中的每一行.我用gcc 4.6.1在我的电脑上测试过它; 它工作正常.
#include <iostream>
#include <fstream>
#include <string>
void f()
{
std::string line;
while(std::getline(std::cin, line)) //input from the file in.txt
{
std::cout << line << "\n"; //output to the file out.txt
}
}
int main()
{
std::ifstream in("in.txt");
std::streambuf *cinbuf = std::cin.rdbuf(); //save old buf
std::cin.rdbuf(in.rdbuf()); //redirect std::cin to in.txt!
std::ofstream out("out.txt");
std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
std::cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
std::string word;
std::cin >> word; //input from the file in.txt
std::cout << word << " "; //output to the file out.txt
f(); //call function
std::cin.rdbuf(cinbuf); //reset to standard input again
std::cout.rdbuf(coutbuf); //reset to standard output again
std::cin >> word; //input from the standard input
std::cout << word; //output to the standard input
}
Run Code Online (Sandbox Code Playgroud)
您可以在一行中保存和重定向:
auto cinbuf = std::cin.rdbuf(in.rdbuf()); //save and redirect
Run Code Online (Sandbox Code Playgroud)
这里std::cin.rdbuf(in.rdbuf())设置std::cin'sbuffer to in.rdbuf()然后返回与之关联的旧缓冲区std::cin.可以使用std::cout- 或任何流来完成同样的事情.
希望有所帮助.
Tso*_*dze 84
写吧
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
freopen("output.txt","w",stdout);
cout<<"write in file";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
aei*_*eid 16
假设您的编译程序名称是x.exe,$是系统shell或提示符
$ x <infile >outfile
Run Code Online (Sandbox Code Playgroud)
将从infile获取输入并输出到outfile.
Vad*_*zim 16
这是一个用于阴影cin/cout的简短代码片段,可用于编程竞赛:
#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;
}
Run Code Online (Sandbox Code Playgroud)
这提供了额外的好处,即普通fstream比同步的stdio流更快.但这仅适用于单一功能的范围.
全局cin/cout重定向可以写成:
#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();
}
Run Code Online (Sandbox Code Playgroud)
请注意,ios_base::sync_with_stdio也重置std::cin.rdbuf.所以订单很重要.
另见ios_base :: sync_with_stdio的意义(false); cin.tie(NULL);
对于单个文件的范围,Std io流也可以很容易地被遮蔽,这对竞争性编程很有用:
#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();
}
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我们必须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
^
Run Code Online (Sandbox Code Playgroud)
另请参见如何在C++中正确使用名称空间?,为什么"使用命名空间std"被认为是不好的做法?以及如何解决C++命名空间和全局函数之间的名称冲突?
试试这个将cout重定向到文件。
#include <iostream>
#include <fstream>
int main()
{
/** backup cout buffer and redirect to out.txt **/
std::ofstream out("out.txt");
auto *coutbuf = std::cout.rdbuf();
std::cout.rdbuf(out.rdbuf());
std::cout << "This will be redirected to file out.txt" << std::endl;
/** reset cout buffer **/
std::cout.rdbuf(coutbuf);
std::cout << "This will be printed on console" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
阅读全文使用 std::rdbuf 重定向 cin 和 cout
| 归档时间: |
|
| 查看次数: |
163783 次 |
| 最近记录: |