stdout和STDOUT_FILENO之间的区别 [英] The difference between stdout and STDOUT_FILENO
问题描述
我想知道Linux C中 stdout
和 STDOUT_FILENO
之间的区别.
I was wondering the difference between stdout
and STDOUT_FILENO
in Linux C.
经过一些搜索工作,我得出以下结论.您能帮我查看一下并更正其中的任何错误吗?谢谢
After some searching work, I draw the following conclusion. Could you help me review it and correct any mistake in it? Thanks
-
stdout
属于C语言的标准I/O流;类型为FILE *并在stdio.h中定义的
stdout
belongs to standard I/O stream of C language; whose type is FILE* and defined in stdio.h
STDOUT_FILENO
在 unistd.h
处定义.它是LINUX系统的文件描述符.在 unistd.h
中,解释如下:
STDOUT_FILENO
, possessing an int type, is defined at unistd.h
. It's a file descriptor of LINUX system. In unistd.h
, it's explained as below:
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.
因此,我认为 STDOUT_FILENO
属于系统级调用,并且在某种程度上类似于系统API. STDOUT_FILENO
可用于描述系统中的任何设备.
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
位于较高的级别(用户级别?),并且实际上封装了 STDOUT_FILENO
的详细信息. stdout
具有I/O缓冲区.
The stdout
locates in a higher level (user level?) and actually encapsulate the details of STDOUT_FILENO
. stdout
has I/O buffer.
我了解他们之间的区别.谢谢您的任何评论或纠正.
That's my understand about their difference. Any comment or correction is appreciated, thanks.
推荐答案
stdout
是 FILE *
"constant",提供标准输出流.因此,显然 fprintf(stdout,"x =%d \ n",x);
具有与 printf("x =%d \ n",x);相同的行为>;您将
stdout
用于< stdio.h>
函数,例如 fprintf
, fputs
等.
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_FILENO
是一个整数文件描述符(实际上是整数1).您可以将其用于 write
系统调用.
STDOUT_FILENO
is an integer file descriptor (actually, the integer 1). You might use it for write
syscall.
两者之间的关系是 STDOUT_FILENO == fileno(stdout)
(除了在执行诸如 fclose(stdout);
之类的怪异事情之后,或者在某些 fclose(stdin)
freopen 之外,>,您几乎应该永远不要这样做!请参见此.com/users/4279/jf-sebastian> JFSebastian )
(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)
您通常更喜欢 FILE *
东西,因为它们是经过缓冲的(因此通常表现良好).有时,您可能需要调用 fflush
刷新缓冲区.
You usually prefer the FILE*
things, because they are buffered (so usually perform well). Sometimes, you may want to call fflush
to flush buffers.
您可以为系统调用使用文件描述符编号,例如投票(2).但是使用系统调用很麻烦.它可能会给您带来很好的效率(但是很难编写代码),但是 stdio
库通常足够好(并且更易于移植).
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).
(当然,对于stdio函数,您应该 #include< stdio.h>
,以及 #include< unistd.h>
-和其他一些标头-对于像 write
这样的系统调用,并且stdio函数是通过syscalls实现的,因此 fprintf
可以调用 write
).
(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
).
这篇关于stdout和STDOUT_FILENO之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!