C语言 stdin 和 STDIN_FILENO 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15102992/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:28:27  来源:igfitidea点击:

What is the difference between stdin and STDIN_FILENO?

c

提问by user1212697

What is the practical difference, if any, between stdinand STDIN_FILENOin C?

CstdinSTDIN_FILENOC之间的实际区别是什么(如果有的话)?

回答by Michael

The interface. Like everyone else has said, stdinis a FILE *as defined by the standard c library. You can use some of the higher level interfaces like fread, fwrite, and fprintf. On the other hand, STDIN_FILENOis just a file descriptor (almost certainly 0). This uses a slight lower level interface through the likes of readand write.

界面。就像其他人所说的那样,stdinFILE *标准 c 库定义的 a 。你可以使用一些像更高级别的接口freadfwritefprintf。另一方面,STDIN_FILENO它只是一个文件描述符(几乎可以肯定为 0)。这通过read和 之类的方式使用了一个稍低级别的接口write

回答by squiguy

stdinis a default FILE pointer used to get input from none other than standard in.

stdin是一个默认的 FILE 指针,用于从标准输入中获取输入。

STDIN_FILENOis the default standard input file descriptor number which is 0. It is essentially a defined directive for general use.

STDIN_FILENO是默认的标准输入文件描述符编号,即0. 它本质上是一个通用的定义指令。

回答by debug

From /usr/include/stdio.h,

/usr/include/stdio.h,

/* Standard streams.  */
extern struct _IO_FILE *stdin;          /* Standard input stream.  */
extern struct _IO_FILE *stdout;         /* Standard output stream.  */
extern struct _IO_FILE *stderr;         /* Standard error output stream.  */
/* C89/C99 say they're macros.  Make them happy.  */
#define stdin stdin
#define stdout stdout
#define stderr stderr

From /usr/include/unistd.h

/usr/include/unistd.h

/* Standard file descriptors.  */
#define STDIN_FILENO    0       /* Standard input.  */
#define STDOUT_FILENO   1       /* Standard output.  */
#define STDERR_FILENO   2       /* Standard error output.  */

Ex, stdin(_IO_FILEdefined in /usr/include/libio.h) is a structure data. STDIN_FILENOis a macro constant, which points to a file descriptor used by kernel.

例如,stdin_IO_FILE在 中定义/usr/include/libio.h)是结构数据。STDIN_FILENO是一个宏常量,指向内核使用的文件描述符。

#include <stdio.h>
#include <unistd.h>

void
stdin_VS_STDIN_FILENO(void)
{
    printf("stdin->_flags = %hd\n", stdin->_flags);
    printf("STDIN_FILENO  : %d\n", STDIN_FILENO);
}

int
main(void)
{
    stdin_VS_STDIN_FILENO();
    return 0;
}

回答by Udhay Devgotra

stdin : 1. A file pointer (* FILE) 2. The file descriptor table holds its address when process is created. 3. present in /usr/include/stdio.h

stdin : 1. 文件指针 (* FILE) 2. 文件描述符表在创建进程时保存其地址。3.存在于/usr/include/stdio.h

STDIN_FILENO : 1. It is a macro 2. Its nothing but an array index of a file descriptor table (default 0). 3.present in /usr/include/unistd.h

STDIN_FILENO : 1. 它是一个宏 2. 它只是文件描述符表的数组索引(默认为 0)。3.存在于/usr/include/unistd.h

Could be more clear by following code.

可以通过以下代码更清楚。

#include<stdio.h> 
#include<unistd.h> 
int main() 
{    
     printf("%d\t\t%p ----- ",STDIN_FILENO,stdin);

return 0; 
}