C语言 在 C 中写入 stdout 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16430108/
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-09-02 06:17:28  来源:igfitidea点击:

What does it mean to write to stdout in C?

cunixstdout

提问by Acroyear

Does a program that writes to "stdout" write to a file? the screen? I don't understand what it means to write to stdout.

写入“stdout”的程序是否写入文件?屏幕?我不明白写入标准输出意味着什么。

回答by K Scott Piel

That means that you are printing output on the main output device for the session... whatever that may be. The user's console, a tty session, a file or who knows what. What that device may be varies depending on how the program is being run and from where.

这意味着您正在会话的主输出设备上打印输出......无论是什么。用户的控制台、tty 会话、文件或知道什么。该设备可能是什么,这取决于程序的运行方式和位置。

The following command will write to the standard output device (stdout)...

以下命令将写入标准输出设备(stdout)...

printf( "hello world\n" );

Which is just another way, in essence, of doing this...

这只是另一种方式,本质上,这样做......

fprintf( stdout, "hello world\n" );

In which case stdoutis a pointer to a FILEstream that represents the default output device for the application. You could also use

在这种情况下,stdout是指向FILE表示应用程序默认输出设备的流的指针。你也可以使用

fprintf( stderr, "that didn't go well\n" );

in which case you would be sending the output to the standard error output device for the application which may, or may not, be the same as stdout-- as with stdout, stderris a pointer to a FILEstream representing the default output device for error messages.

在这种情况下,您会将输出发送到应用程序的标准错误输出设备,它可能与也可能不同stdout——与 一样stdoutstderr是指向FILE表示错误消息的默认输出设备的流的指针。

回答by akivajgordon

It depends.

这取决于。

When you commit to sending output to stdout, you're basically leaving it up to the user to decide where that output should go.

当您承诺将输出发送到 时stdout,您基本上是让用户来决定该输出的去向。

If you use printf(...)(or the equivalent fprintf(stdout, ...)), you're sending the output to stdout, but where that actually ends up can depend on how I invoke your program.

如果您使用printf(...)(或等效的fprintf(stdout, ...)),则您将输出发送到stdout,但实际结束的位置取决于我如何调用您的程序。

If I launch your program from my console like this, I'll see output on my console:

如果我像这样从我的控制台启动你的程序,我会在我的控制台上看到输出:

$ prog
Hello, World! # <-- output is here on my console

However, I might launch the program like this, producing no output on the console:

但是,我可能会像这样启动程序,在控制台上不产生任何输出:

$ prog > hello.txt

but I would now have a file "hello.txt" with the text "Hello, World!" inside, thanks to the shell's redirection feature.

但我现在有一个文件“hello.txt”,文本为“Hello, World!” 在里面,感谢 shell 的重定向功能。

Who knows – I might even hook up some other device and the output could go there. The point is that when you decide to print to stdout(e.g. by using printf()), then you won't exactly know where it will go until you see how the process is launched or used.

谁知道——我什至可以连接一些其他设备,输出可以去那里。关键是当您决定打印到stdout(例如通过使用printf())时,您将无法确切知道它将去哪里,直到您看到该进程是如何启动或使用的。

回答by Alex Cutajar

stdout is the standard output file stream. Obviously, it's first and default pointer to output is the screen, however you can point it to a file as desired!

stdout 是标准输出文件流。显然,它的第一个和默认输出指针是屏幕,但是您可以根据需要将其指向文件!

Please read:

请阅读:

http://www.cplusplus.com/reference/cstdio/stdout/

http://www.cplusplus.com/reference/cstdio/stdout/

C++ is very similar to C however, object oriented.

C++ 与 C 非常相似,但都是面向对象的。

回答by A.E. Drew

stdoutis the standard output stream in UNIX. See http://www.gnu.org/software/libc/manual/html_node/Standard-Streams.html#Standard-Streams. When running in a terminal, you will see data written to stdoutin the terminal and you can redirect it as you choose.

stdout是 UNIX 中的标准输出流。请参阅http://www.gnu.org/software/libc/manual/html_node/Standard-Streams.html#Standard-Streams。在终端中运行时,您将看到写入stdout终端的数据,您可以根据需要重定向它。

回答by Hyman

stdoutstands for standard output streamand it is a stream which is available to your program by the operating system itself. It is already available to your program from the beginning together with stdinand stderr.

stdout代表标准输出流,它是操作系统本身可用于您的程序的流。它从一开始就可以与stdin和一起用于您的程序stderr

What they point to (or from) can be anything, actually the stream just provides your program an object that can be used as an interface to send or retrieve data. By default it is usually the terminal but it can be redirected wherever you want: a file, to a pipe goint to another process and so on.

它们指向(或来自)的东西可以是任何东西,实际上流只是为您的程序提供了一个对象,该对象可用作发送或检索数据的接口。默认情况下,它通常是终端,但它可以重定向到任何你想要的地方:一个文件,一个指向另一个进程的管道等等。

回答by Gabriel Staples

@K Scott Piel wrote a great answer here, but I want to add one important point.

@K Scott Piel在这里写了一个很好的答案,但我想补充一点。

Note that the stdoutstream is usually line-buffered, so to ensure the output is actually printed and not just left sitting in the buffer waiting to be written you mustflush the buffer by either ending your printfstatement with a \n

请注意,stdout通常是行缓冲的,因此为了确保实际打印输出而不只是留在缓冲区中等待写入,您必须通过以下任一方式结束您的printf语句刷新缓冲区\n

Ex:

前任:

printf("hello world\n");

or

或者

printf("hello world"); 
printf("\n");

or similar, OR you must call fflush(stdout);after your printfcall.

或类似的,或者您必须在通话fflush(stdout);printf致电。

Ex:

前任:

printf("hello world"); 
fflush(stdout);

Read more here: Why does printf not flush after the call unless a newline is in the format string?

在此处阅读更多信息:除非换行符在格式字符串中,否则为什么 printf 在调用后不会刷新?