C++ 如何将cin和cout重定向到文件?
如何将C++ 如何将cin和cout重定向到文件?,c++,C++,如何将cin重定向到in.txt和cout重定向到out.txt?下面是一个您想要做的工作示例。阅读注释,了解代码中每一行的作用。我已经用GCC4.6.1在我的电脑上进行了测试;它很好用 #include <iostream> #include <fstream> #include <string> void f() { std::string line; while(std::getline(std::cin, line)) //inpu
cin
重定向到in.txt
和cout
重定向到out.txt
?下面是一个您想要做的工作示例。阅读注释,了解代码中每一行的作用。我已经用GCC4.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
}
这里std::cin.rdbuf(in.rdbuf())
将std::cin的
缓冲区设置为in.rdbuf()
,然后返回与std::cin
关联的旧缓冲区。同样的情况也可以通过std::cout
-或任何流来实现
希望这能有所帮助。写吧
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
freopen("output.txt","w",stdout);
cout<<"write in file";
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
freopen(“output.txt”,“w”,stdout);
cout假设编译程序名为x.exe
$是系统shell或提示符
$ x <infile >outfile
$x输出文件
将从infle获取输入,并输出到outfile。下面是一个简短的代码片段,用于跟踪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;
}
但在这种情况下,我们必须逐个选择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
^
另请参见,并尝试将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;
}
#包括
#包括
int main()
{
/**备份cout缓冲区并重定向到out.txt**/
std::ofstreamout(“out.txt”);
auto*coutbuf=std::cout.rdbuf();
std::cout.rdbuf(out.rdbuf());
如果您的输入文件是in.txt,那么可以使用freopen将stdin文件设置为in.txt
freopen("in.txt","r",stdin);
如果要对输出执行相同的操作:
freopen("out.txt","w",stdout);
这将适用于std::cin(如果使用c++)、printf等
这也将帮助您在clion、vscode中调试代码
编辑
如果你想重置标准输入
fclose(stdin);
stdin = fdopen(0, "r"); //reopen: 0 is file descriptor of std input
和重置标准输出
fclose(stdout);
stdout = fdopen(1, "w"); //reopen: 1 is file descriptor of std output
C语言中的I/O重定向++
//将cout重定向到文件的Cpp程序
#包括
#包括
#包括
使用名称空间std;
int main()
{
流文件;
打开(“cout.txt”,ios::out);
弦线;
//cout的备份流缓冲区
streambuf*stream_buffer_cout=cout.rdbuf();
streambuf*stream_buffer_cin=cin.rdbuf();
//获取文件的streambuffer
streambuf*stream_buffer_file=file.rdbuf();
//将cout重定向到文件
cout.rdbuf(流缓冲区文件);
cout Redirect cin to a string:-Redirect cout to a string:在将cin和cout重置为标准IO之前,我是否需要关闭文件?@updogliu:否。如果需要,您可以使用in
和out
读取和写入,in.txt
和out.txt
。此外,当in
和out
超出范围。我喜欢这个解决方案而不是freopen
解决方案,因为如果我使用freopen
,我再也无法找回我的stdout
。这是重定向stdout
,而不是cout
。这也会重定向printf,在某些情况下可能是件好事。@AkshayLAradhya not w当你设置的时候,默认情况下它被设置为<代码>真的。@ P·EysL.astn。为什么?如果答案在功能上是相等的,那么C++的等价物将是<>代码> OfSufout(“out .txt”);cout。rdBuf(out。rdBuf())
-只多了一行,而且便于携带。不那么简单:)这个问题在6年前(2012年)就已经得到了回答,但您现在已经在2018年添加了一个答案。您的答案与已接受的答案相同。因此,我想知道,您为什么在没有任何新内容可添加的情况下发布此消息?我的答案仅特别强调cout版本,详细答案在下面的链接中提供。您的答案中有哪些新内容未出现在已接受的答案中?我的答案不混合CUT和CIN的重定向,我的版本分离,使它更容易读,而不是C++相关的,在任何非平凡的例子中都失败了,例如当程序生成了写入控制台的子进程。至少这是我尝试重定向时遇到的问题,因此我为什么在这里。code>stdout
在这种情况下?@Broxigar编辑以添加有关重置的信息您好,欢迎来到Stackoverflow。您回答了一个非常老的问题,有很多答案。请回答您的问题并添加解释,说明为什么此答案比其他答案更好
freopen("out.txt","w",stdout);
fclose(stdin);
stdin = fdopen(0, "r"); //reopen: 0 is file descriptor of std input
fclose(stdout);
stdout = fdopen(1, "w"); //reopen: 1 is file descriptor of std output
// Cpp program to redirect cout to a file
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
fstream file;
file.open("cout.txt", ios::out);
string line;
// Backup streambuffers of cout
streambuf* stream_buffer_cout = cout.rdbuf();
streambuf* stream_buffer_cin = cin.rdbuf();
// Get the streambuffer of the file
streambuf* stream_buffer_file = file.rdbuf();
// Redirect cout to file
cout.rdbuf(stream_buffer_file);
cout << "This line written to file" << endl;
// Redirect cout back to screen
cout.rdbuf(stream_buffer_cout);
cout << "This line is written to screen" << endl;
file.close();
return 0;
}