LINUX C中stdout和STDOUT_FILENO的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12902627/
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
The difference between stdout and STDOUT_FILENO in LINUX C
提问by Bing Lu
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_FILENO
, possessing an int type, is defined atunistd.h
. It's a file descriptor of LINUX system. Inunistd.h
, it's explained as below:
stdout
属于C语言的标准I/O流;其类型为 FILE* 并在 stdio.h 中定义STDOUT_FILENO
,拥有一个 int 类型,在 处定义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.
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.
这就是我对它们差异的理解。任何意见或更正表示赞赏,谢谢。
采纳答案by Basile Starynkevitch
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
是FILE*
给出标准输出流的“常量”。所以显然fprintf(stdout, "x=%d\n", x);
具有相同的行为printf("x=%d\n", x);
;您stdout
用于,等<stdio.h>
功能。fprintf
fputs
STDOUT_FILENO
is an integer file descriptor (actually, the integer 1). You might use it for write
syscall.
STDOUT_FILENO
是一个整数文件描述符(实际上是整数 1)。您可以将它用于write
系统调用。
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);
,或者一些freopen
之后fclose(stdin)
,你几乎不应该做的事情!看到这个,正如JFSebastian评论的那样)
You usually prefer the FILE*
things, because they are buffered (so usually perform well). Sometimes, you may want to call fflush
to flush buffers.
你通常更喜欢这些FILE*
东西,因为它们是缓冲的(所以通常表现良好)。有时,您可能想要调用fflush
刷新缓冲区。
You could use file descriptor numbers for syscallslike 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)。但是使用系统调用是笨拙的。它可能会给您带来非常好的效率(但这很难编码),但通常该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 <stdio.h>
针对 stdio 函数,以及#include <unistd.h>
- 以及其他一些标头 - 用于系统调用,例如write
。并且 stdio 函数是使用系统调用实现的,因此fprintf
可以调用write
)。