在LINUX下C stdout和STDOUT_FILENO之间的区别 [英] The difference between stdout and STDOUT_FILENO in LINUX C

查看:813
本文介绍了在LINUX下C stdout和STDOUT_FILENO之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道的区别标准输出 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 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 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 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.

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 of STDOUT_FILENO. stdout has I/O buffer.

That's my understand about their difference. Any comment or correction is appreciated, thanks.

解决方案

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 is an integer file descriptor (actually, the integer 1). You might use it for write syscall.

The relation between the two is 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)

You usually prefer the FILE* things, because they are buffered (so usually perform well). Sometimes, you may want to call fflush 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 the stdio 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 like write. And the stdio functions are implemented with syscalls, so fprintf may call write).

这篇关于在LINUX下C stdout和STDOUT_FILENO之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆