freopen() 等效于 C++ 流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5257509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:47:10  来源:igfitidea点击:

freopen() equivalent for c++ streams

c++cio

提问by flight

When programming with c-style i/o I sometimes use freopen() to reopen stdin for testing purposes so that I don't have to retype the input over and over. I was wondering if there is an equivalent for c++ i/o streams. Also, I know that I can use pipes to redirect it on the command line/terminal/whateveritis but I was wondering if there was a way to do it inside my code (because as you can see, I'm not very knowledgeable about the cl/t/w).

当使用 c 风格的 i/o 编程时,我有时会使用 freopen() 重新打开 stdin 以进行测试,这样我就不必一遍又一遍地重新输入输入。我想知道 c++ i/o 流是否有等价物。另外,我知道我可以使用管道在命令行/终端/任何东西上重定向它,但我想知道是否有办法在我的代码中做到这一点(因为如你所见,我对升/吨/瓦)。

回答by UmmaGumma

freopenalso works with cinand cout. No need to search for something new.

freopen也适用于cincout。无需寻找新的东西。

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

Edit:From C++ standard 27.3.1:

编辑:来自 C++ 标准 27.3.1:

The object cin controls input from a stream buffer associated with the object stdin, declared in <cstdio>.

对象 cin 控制来自与对象 stdin 关联的流缓冲区的输入,在 中声明<cstdio>

So according to the standard, if we redirect stdinit will also redirect cin. Vice versa for cout.

所以根据标准,如果我们重定向stdin它也会重定向cin. 反之亦然cout

回答by Rob?

#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";
}

http://www.cplusplus.com/reference/iostream/ios/rdbuf/

http://www.cplusplus.com/reference/iostream/ios/rdbuf/

回答by Lightness Races in Orbit

Thisnewsgroup posting explores your options.

新闻组帖子探讨了您的选择。

This is system dependent and the poster didn't indicate the system, but cin.clear() should work. I have tested the attached program on a UNIX system with AT&T version's of iostreams.

这是系统相关的,海报没有指出系统,但 cin.clear() 应该可以工作。我已经在带有 AT&T 版本的 iostreams 的 UNIX 系统上测试了附加的程序。

#include <iostream.h>
int main()
{
    for(;;) {
        if ( cin.eof() ) {
            cout << "EOF" << endl;
            cin.clear();
        }
        char c ;
        if ( cin.get(c) ) cout.put(c) ;
    }
} 

Yes, that works okay in cfront and TC++. In g++ where the problem first arose an additional action is required:

是的,这在 cfront 和 TC++ 中工作正常。在首先出现问题的 g++ 中,需要额外的操作:

  cin.clear();
  rewind ( _iob ); // Seems quite out of place, doesn't it?
                   // cfront also accepts but doesn't
                   // require this rewind. 

Though I note that this was in 1991, it should still work. Remember to use the now-standard iostreamheader, not iostream.h.

虽然我注意到这是在 1991 年,但它应该仍然有效。请记住使用现在标准的iostream标头,而不是iostream.h.

(BTW I found that post with the Google search terms "reopen cin c++", second result.)

(顺便说一句,我发现该帖子带有 Google 搜索词“reopen cin c++”,第二个结果。)

Let us know how you get on. You could also just use freopen.

让我们知道您的身体情况如何。你也可以只使用freopen.