在LINUX下C stdout和STDOUT_FILENO之间的区别 [英] The difference between stdout and STDOUT_FILENO in LINUX C
问题描述
我想知道的区别标准输出
和 STDOUT_FILENO
在Linux下C。
在一些搜索工作中,我得出以下结论。你能帮助我审查,并纠正任何错误呢?谢谢
-
标准输出
属于标准I / C语言O流;其类型是FILE *在stdio.h中定义 -
STDOUT_FILENO
,拥有一个int类型,在unistd.h中定义
。这是LINUX系统的文件描述符。在unistd.h中
,它的解释如下:
下列符号常量应当文件流的定义:STDERR_FILENO
文件标准错误的数量; 2。
STDIN_FILENO
标准输入的文件编号; 0。
STDOUT_FILENO
文件标准输出数量; 1。
块引用>所以,在我看来,在
STDOUT_FILENO
属于系统级的呼叫,并在一定程度上,就像一个系统API。STDOUT_FILENO
可以用来描述任何设备系统。的
标准输出
位于较高水平(用户级?),实际封装STDOUT_FILENO
的细节。标准输出
具有I / O缓冲区。这就是我理解他们的区别。任何意见或修正AP preciated,谢谢。
解决方案
标准输出
是FILE *
常数让标准outout流。所以很明显fprintf中(标准输出,X =%d个\\ N,X);
具有相同的行为为的printf(x =%d个\\ N,X);
;您使用标准输出
为< stdio.h中>
如函数fprintf中
,的fputs
等
STDOUT_FILENO
是一个整数文件描述符(实际上,整数1)。你可以使用它的写
系统调用。这两者之间的关系是
STDOUT_FILENO ==的fileno(标准输出)
(除了后你不喜欢
FCLOSE奇怪的事情(标准输出);
,或者一些则freopen
经过一番FCLOSE(标准输入)
,你应该几乎不这么做!请参见这个,由 JFSebastian 评论)您通常preFER的
FILE *
的东西,因为他们是缓冲(所以平时表现良好)。有时候,你可能需要调用fflush
刷新缓冲区。您可以使用文件描述符号码系统调用喜欢的write(2)(用于由
STDIO
库)或调查(2)。但是,使用系统调用是clumpsy。它可能会给你很好的效率(但是这是很难code),但很多时候在STDIO
库是足够好(更便携)。(当然你应该
的#include< stdio.h中>
为标准输入输出功能,以及的#include< unistd.h中>
- 和其他一些包头中的系统调用像写
,而且标准输入输出功能与系统调用实现的,所以fprintf中
可致电写
)。I was wondering the difference between
stdout
andSTDOUT_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
belongs to standard I/O stream of C language; whose type is FILE* and defined in stdio.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: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.The
stdout
locates in a higher level (user level?) and actually encapsulate the details ofSTDOUT_FILENO
.stdout
has I/O buffer.That's my understand about their difference. Any comment or correction is appreciated, thanks.
解决方案
stdout
is aFILE*
"constant" giving the standard outout stream. So obviouslyfprintf(stdout, "x=%d\n", x);
has the same behavior asprintf("x=%d\n", x);
; you usestdout
for<stdio.h>
functions likefprintf
,fputs
etc..
STDOUT_FILENO
is an integer file descriptor (actually, the integer 1). You might use it forwrite
syscall.The relation between the two is
STDOUT_FILENO == fileno(stdout)
(Except after you do weird things like
fclose(stdout);
, or perhaps somefreopen
after somefclose(stdin)
, which you should almost never do! See this, as commented by J.F.Sebastian)You usually prefer the
FILE*
things, because they are buffered (so usually perform well). Sometimes, you may want to callfflush
to flush buffers.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 thestdio
library is good enough (and more portable).(Of course you should
#include <stdio.h>
for the stdio functions, and#include <unistd.h>
-and some other headers- for the syscalls likewrite
. And the stdio functions are implemented with syscalls, sofprintf
may callwrite
).这篇关于在LINUX下C stdout和STDOUT_FILENO之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!