是否可以在将 cout 重定向到 outfile 的同时 cout 到终端?

发布于 2024-08-19 13:30:46 字数 265 浏览 7 评论 0原文

我正在运行一个程序并将 cout 重定向到一个输出文件,如下所示:

./program < infile.in > outfile.o  

我希望能够从命令行读取选项(“-h”或“--help”)并向终端输出帮助消息。有没有一种方法可以做到这一点,但仍然将程序其余部分的常规 cout 转到输出文件?

cout 是用于此类事情的正确对象吗?

I'm running a program and redirecting cout to an outfile, like so:

./program < infile.in > outfile.o  

I want to be able to read in an option ('-h' or '--help') from the command line and output a help message to the terminal. Is there a way I can do this but still have the regular cout from the rest of the program go to the outfile?

Would cout be the right object to use for such a thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

〃安静 2024-08-26 13:30:46

您应该使用 cerr 将帮助消息输出到 STDERR,该消息不包含在到 outfile.o 的重定向中。

给定 ./program < infile.in > outfile.o

cout << "This writes to STDOUT, and gets redirected to outfile.";
cerr << "This doesn't get redirected, and displays on screen.";

如果稍后您想同时重定向 STDOUT 和 STDERR,则可以这样做

./program < infile.in &> outfile.o

如果您只想重定向 STDERR,但允许显示 STDOUT,请使用

./program < infile.in 2> outfile.o

Bash 重定向 比大多数人意识到的更复杂,通常除了最简单的形式(“>”)之外的所有内容被忽视。

You should use cerr to output your help message to STDERR, which is not included in your redirection to outfile.o.

Given ./program < infile.in > outfile.o:

cout << "This writes to STDOUT, and gets redirected to outfile.";
cerr << "This doesn't get redirected, and displays on screen.";

If, later on, you want to redirect both STDOUT and STDERR, you can do

./program < infile.in &> outfile.o

If you want to redirect only STDERR, but allow STDOUT to display, use

./program < infile.in 2> outfile.o

Bash redirection is more complex than most people realize, and often everything except the simplest form (">") gets overlooked.

对岸观火 2024-08-26 13:30:46

如果您使用的是 Linux,则可以使用伪设备 /dev/tty 输出到控制终端(如果有)。即使 stderrstdout 都被重定向,这也将起作用。其他操作系统可能提供类似的机制。

例如,

#include <iostream>
#include <ostream>
#include <fstream>

int main()
{
        std::ofstream term("/dev/tty", std::ios_base::out);

        term << "This goes to terminal\n";
        std::cout << "This goes to stdout\n";

        return 0;
}

将像这样工作:

$ ./a.out
This goes to stdout
This goes to terminal
$ ./a.out >/dev/null
This goes to terminal

请注意,如果两个流独立缓冲,则如果它们输出到同一设备,则不一定保留相对顺序。这可以通过在适当的时间冲洗流来调整。

If you're on linux you can use the pseudo device /dev/tty to output to a controlling terminal (if any). This will work even if stderr is redirected as well as stdout. Other operating systems may provide similar mechanisms.

E.g.

#include <iostream>
#include <ostream>
#include <fstream>

int main()
{
        std::ofstream term("/dev/tty", std::ios_base::out);

        term << "This goes to terminal\n";
        std::cout << "This goes to stdout\n";

        return 0;
}

Will work like this:

$ ./a.out
This goes to stdout
This goes to terminal
$ ./a.out >/dev/null
This goes to terminal

Note the way that the with the two streams being buffered independently the relative ordering if they are outputting to the same device is not necessarily preserved. This can be adjusted by flushing the streams at appropriate times.

残疾 2024-08-26 13:30:46

<代码>~$ cmd | tee log_file 将 stdout 复制到文件终端
~$ cmd 2>log_filestdout 打印到终端并将 stderr 打印到文件中

~$ cmd | tee log_file to dup stdout to file and terminal
~$ cmd 2>log_file to print stdout onto terminal and stderr into a file

无妨# 2024-08-26 13:30:46

您可能想将帮助消息输出到 stderr。 Stderr 通常用于非正常输出,您可以将使用段落视为此类输出。

You may like to output the help message to stderr. Stderr is generally used for non-normal output and you may consider a usage paragraph to be such output.

凶凌 2024-08-26 13:30:46

我所做的一件事(并不是说这总是合适的)是编写具有类似此签名的模块。

void write_out(ostream &o);

然后我可以创建 fstream 对象并将它们传入,或者传入 cout 和 cerr,无论我当时需要什么。这对于编写日志记录代码很有帮助,有时您想查看终端上发生的情况,而有时您只需要一个日志文件。

HTH。

One of the things I've done - not saying this is always appropriate - is write modules that have something like this signature.

void write_out(ostream &o);

And then I can create fstream objects and pass them in, or pass in cout and cerr, whatever I need to at that time. This can be helpful in writing logging code where sometimes you want to see on-terminal what happens, and at other times you just want a logfile.

HTH.

说好的呢 2024-08-26 13:30:46

您应该使用 cerr 而不是 cout。使用 shell 重定向>只重定向 stdout (cout),而不重定向 stderr (cerr)。

You should use cerr instead of cout. Using shell redirection > only redirects stdout (cout), not stderr (cerr).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文