在使用c风格的输入输出编程时,有时我会使用freopen()来重新打开stdin以进行测试,这样我就不必一遍又一遍地重新输入。我想知道是否有类似于c++输入输出流的等效方法。此外,我知道可以使用管道在命令行/终端/任何其他地方重定向它,但我想知道是否有一种方法可以在我的代码中完成(因为你可以看到,我对cl/t/w并不是很了解)。
freopen
函数也适用于 cin
和 cout
,无需寻找其他替代品。
freopen("input.txt", "r", stdin); // redirects standard input
freopen("output.txt", "w", stdout); // redirects standard output
int x;
cin >> x; // reads from input.txt
cout << x << endl; // writes to output.txt
编辑: 来自C++标准27.3.1:
对象cin控制与对象stdin相关联的流缓冲区的输入,这在
<cstdio>
中声明。
因此根据标准,如果我们重定向stdin
也会重定向cin
。对于cout
也是如此。
cin 是 istream 类的一个对象,代表标准输入流。它对应于 cstdio 流 stdin。
因此,cin 与 stdin 相关联,如果我们重定向了 stdin
,那么也会重定向 cin
。同样的道理也适用于 cout
。http://www.cplusplus.com/reference/iostream/cin/ - UmmaGummastd::sync_with_stdio(false);
来禁用这种行为,这样 C++ 就可以自己进行缓冲。如果你使用了 std::sync_with_stdio(false)
,你仍然可以重定向 std::cout
,请参见这个相关问题的答案。 - yyny#include <iostream>
#include <fstream>
int main() {
// Read one line from stdin
std::string line;
std::getline(std::cin, line);
std::cout << line << "\n";
// Read a line from /etc/issue
std::ifstream issue("/etc/issue");
std::streambuf* issue_buf = issue.rdbuf();
std::streambuf* cin_buf = std::cin.rdbuf(issue_buf);
std::getline(std::cin, line);
std::cout << line << "\n";
// Restore sanity and read a line from stdin
std::cin.rdbuf(cin_buf);
std::getline(std::cin, line);
std::cout << line << "\n";
}
这篇新闻组帖子探讨了您的选择。
这取决于系统,发帖人没有指明系统,但cin.clear()应该可以工作。我已经在一个UNIX系统上测试了附加的程序,使用的是AT&T版本的iostreams。
#include <iostream.h>
int main()
{
for(;;) {
if ( cin.eof() ) {
cout << "EOF" << endl;
cin.clear();
}
char c ;
if ( cin.get(c) ) cout.put(c) ;
}
}
cin.clear();
rewind ( _iob ); // Seems quite out of place, doesn't it?
// cfront also accepts but doesn't
// require this rewind.
虽然我注意到这是在1991年,但它仍然有效。记得使用现在标准的头文件,而不是。
(顺便说一下,我用谷歌搜索词“reopen cin c++”找到了这篇文章,第二个结果。)
让我们知道你的进展如何。你也可以使用。
cin
重新打开 STDIN 的问题。 - Lightness Races in Orbit