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”的程序会写入文件吗?屏幕吗?我不明白给stdout写信是什么意思。
6 个解决方案
#1
41
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 stdout
is a pointer to a FILE
stream that represents the default output device for the application. You could also use
在这种情况下,stdout是指向文件流的指针,它表示应用程序的默认输出设备。您还可以使用
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
, stderr
is a pointer to a FILE
stream representing the default output device for error messages.
在这种情况下,您将把输出发送到应用程序的标准错误输出设备,该设备可能与stdout相同,也可能与stdout相同——与stdout一样,stderr是一个指向文件流的指针,该文件流表示错误消息的默认输出设备。
#2
10
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.
但是我现在有一个文件“你好”。txt“与文本“你好,世界!”在里面,感谢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()))时,您将无法确切地知道它将走向何处,直到您看到如何启动或使用这个过程。
#3
2
stdout
stands for standard output stream and 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 stdin
and 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.
它们指向(或来自)的对象可以是任何东西,实际上流只是为程序提供一个对象,可以用作发送或检索数据的接口。默认情况下,它通常是终端,但它可以被重定向到您想要的任何地方:一个文件、一个管道goint到另一个进程等等。
#4
1
stdout
is 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 stdout
in the terminal and you can redirect it as you choose.
stdout是UNIX中的标准输出流。见http://www.gnu.org/software/libc/manual/html_node/Standard-Streams.html #标准流。当在终端中运行时,您将看到在终端中写入stdout的数据,您可以选择重定向它。
#5
1
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非常相似,但是面向对象。
#6
0
@K Scott Piel wrote a great answer here, but I want to add one important point.
@K Scott Piel在这里写了一个很好的答案,但是我想补充一点。
Note that the stdout
stream 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 must flush the buffer by either ending your printf
statement with a \n
注意,stdout流通常是行缓冲的,因此为了确保输出实际上是被打印的,而不仅仅是留在缓冲区中等待写入,您必须通过使用\n结束printf语句来刷新缓冲区
Ex:
例:
printf("hello world\n");
or
或
printf("hello world");
printf("\n");
or similar, OR you must call fflush(stdout);
after your printf
call.
或者类似,或者你必须叫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在调用之后不刷新,除非换行是在格式字符串中?
#1
41
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 stdout
is a pointer to a FILE
stream that represents the default output device for the application. You could also use
在这种情况下,stdout是指向文件流的指针,它表示应用程序的默认输出设备。您还可以使用
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
, stderr
is a pointer to a FILE
stream representing the default output device for error messages.
在这种情况下,您将把输出发送到应用程序的标准错误输出设备,该设备可能与stdout相同,也可能与stdout相同——与stdout一样,stderr是一个指向文件流的指针,该文件流表示错误消息的默认输出设备。
#2
10
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.
但是我现在有一个文件“你好”。txt“与文本“你好,世界!”在里面,感谢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()))时,您将无法确切地知道它将走向何处,直到您看到如何启动或使用这个过程。
#3
2
stdout
stands for standard output stream and 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 stdin
and 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.
它们指向(或来自)的对象可以是任何东西,实际上流只是为程序提供一个对象,可以用作发送或检索数据的接口。默认情况下,它通常是终端,但它可以被重定向到您想要的任何地方:一个文件、一个管道goint到另一个进程等等。
#4
1
stdout
is 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 stdout
in the terminal and you can redirect it as you choose.
stdout是UNIX中的标准输出流。见http://www.gnu.org/software/libc/manual/html_node/Standard-Streams.html #标准流。当在终端中运行时,您将看到在终端中写入stdout的数据,您可以选择重定向它。
#5
1
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非常相似,但是面向对象。
#6
0
@K Scott Piel wrote a great answer here, but I want to add one important point.
@K Scott Piel在这里写了一个很好的答案,但是我想补充一点。
Note that the stdout
stream 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 must flush the buffer by either ending your printf
statement with a \n
注意,stdout流通常是行缓冲的,因此为了确保输出实际上是被打印的,而不仅仅是留在缓冲区中等待写入,您必须通过使用\n结束printf语句来刷新缓冲区
Ex:
例:
printf("hello world\n");
or
或
printf("hello world");
printf("\n");
or similar, OR you must call fflush(stdout);
after your printf
call.
或者类似,或者你必须叫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在调用之后不刷新,除非换行是在格式字符串中?