I was wondering the difference between stdout
and STDOUT_FILENO
in Linux C.
我想知道Linux C中stdout和STDOUT_FILENO的区别。
After some searching work, I draw the following conclusion. Could you help me review it and correct any mistake in it? Thanks
经过一些研究工作,我得出以下结论。你能帮我复习一下吗?谢谢
-
stdout
belongs to standard I/O stream of C language; whose type is FILE* and defined in stdio.hstdout属于C语言的标准I/O流;谁的类型是文件*和定义在stdi .h
-
STDOUT_FILENO
, possessing an int type, is defined atunistd.h
. It's a file descriptor of LINUX system. Inunistd.h
, it's explained as below:拥有int类型的STDOUT_FILENO在unistd.h中定义。它是LINUX系统的文件描述符。在unistd。h,解释如下:
The following symbolic constants shall be defined for file streams: STDERR_FILENO File number of stderr; 2. STDIN_FILENO File number of stdin; 0. STDOUT_FILENO File number of stdout; 1.
So, in my opinion, the STDOUT_FILENO
belongs system-level calling and, to some extent, like a system API. STDOUT_FILENO
can be used to describe any device in system.
因此,在我看来,STDOUT_FILENO属于系统级调用,并且在某种程度上类似于系统API。STDOUT_FILENO可以用来描述系统中的任何设备。
The stdout
locates in a higher level (user level?) and actually encapsulate the details of STDOUT_FILENO
. stdout
has I/O buffer.
stdout位于更高的级别(用户级别?),实际上封装了STDOUT_FILENO的细节。stdout I / O缓冲区。
That's my understand about their difference. Any comment or correction is appreciated, thanks.
这就是我对他们不同的理解。如有任何意见或更正,我们将不胜感激。
1 个解决方案
#1
57
stdout
is a FILE*
"constant" giving the standard outout stream. So obviously fprintf(stdout, "x=%d\n", x);
has the same behavior as printf("x=%d\n", x);
; you use stdout
for <stdio.h>
functions like fprintf
, fputs
etc..
stdout是一个提供标准输出流的文件*“常量”。显然fprintf(stdout,“x=%d\n”,x);具有与printf相同的行为(“x=%d\n”,x);对于
STDOUT_FILENO
is an integer file descriptor (actually, the integer 1). You might use it for write
syscall.
STDOUT_FILENO是一个整型文件描述符(实际上是整型1),您可以用它来写syscall。
The relation between the two is STDOUT_FILENO == fileno(stdout)
两者之间的关系是STDOUT_FILENO == fileno(stdout)
(Except after you do weird things like fclose(stdout);
, or perhaps some freopen
after some fclose(stdin)
, which you should almost never do! See this, as commented by J.F.Sebastian)
(除非你做了一些奇怪的事情,比如fclose(stdout),或者一些fclose(stdin)之后的freopen (stdin),你几乎不应该这样做!看看这个,正如j.f.塞巴斯蒂安评论的)
You usually prefer the FILE*
things, because they are buffered (so usually perform well). Sometimes, you may want to call fflush
to flush buffers.
您通常喜欢文件*内容,因为它们是缓冲的(所以通常表现良好)。有时,您可能想要调用fflush来刷新缓冲区。
You could use file descriptor numbers for syscalls like write(2) (which is used by the stdio
library), or poll(2). But using syscalls is clumpsy. It may give you very good efficiency (but that is hard to code), but very often the stdio
library is good enough (and more portable).
您可以使用像write(2) (stdio库使用)或poll(2)这样的syscalls的文件描述符编号。但是使用注射器是笨拙的。它可能会给您带来非常好的效率(但这很难编码),但是stdio库通常已经足够好了(并且更加便携)。
(Of course you should #include <stdio.h>
for the stdio functions, and #include <unistd.h>
-and some other headers- for the syscalls like write
. And the stdio functions are implemented with syscalls, so fprintf
may call write
).
(当然你应该#include
#1
57
stdout
is a FILE*
"constant" giving the standard outout stream. So obviously fprintf(stdout, "x=%d\n", x);
has the same behavior as printf("x=%d\n", x);
; you use stdout
for <stdio.h>
functions like fprintf
, fputs
etc..
stdout是一个提供标准输出流的文件*“常量”。显然fprintf(stdout,“x=%d\n”,x);具有与printf相同的行为(“x=%d\n”,x);对于
STDOUT_FILENO
is an integer file descriptor (actually, the integer 1). You might use it for write
syscall.
STDOUT_FILENO是一个整型文件描述符(实际上是整型1),您可以用它来写syscall。
The relation between the two is STDOUT_FILENO == fileno(stdout)
两者之间的关系是STDOUT_FILENO == fileno(stdout)
(Except after you do weird things like fclose(stdout);
, or perhaps some freopen
after some fclose(stdin)
, which you should almost never do! See this, as commented by J.F.Sebastian)
(除非你做了一些奇怪的事情,比如fclose(stdout),或者一些fclose(stdin)之后的freopen (stdin),你几乎不应该这样做!看看这个,正如j.f.塞巴斯蒂安评论的)
You usually prefer the FILE*
things, because they are buffered (so usually perform well). Sometimes, you may want to call fflush
to flush buffers.
您通常喜欢文件*内容,因为它们是缓冲的(所以通常表现良好)。有时,您可能想要调用fflush来刷新缓冲区。
You could use file descriptor numbers for syscalls like write(2) (which is used by the stdio
library), or poll(2). But using syscalls is clumpsy. It may give you very good efficiency (but that is hard to code), but very often the stdio
library is good enough (and more portable).
您可以使用像write(2) (stdio库使用)或poll(2)这样的syscalls的文件描述符编号。但是使用注射器是笨拙的。它可能会给您带来非常好的效率(但这很难编码),但是stdio库通常已经足够好了(并且更加便携)。
(Of course you should #include <stdio.h>
for the stdio functions, and #include <unistd.h>
-and some other headers- for the syscalls like write
. And the stdio functions are implemented with syscalls, so fprintf
may call write
).
(当然你应该#include