什么是文件的内容psented重新$ P $ * [英] What is represented by the content of FILE*
问题描述
我有这个计划。
的#include<&stdio.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&; SYS / socket.h中>
#包括LT&;&fcntl.h GT;INT主要(无效)
{
FILE * F =的fopen(/用户/用户/ a.cc,RB);
的printf(%I \\ N,F); // 1976385616
的printf(%I \\ N,* F); // 1976385768 INT的sockfd =插座(AF_UNIX,SOCK_STREAM,0);
的printf(%I \\ N的sockfd); // 4 FCLOSE(F);
接近(的sockfd); INT FD =打开(/用户/用户/ a.cc,O_TRUNC | O_WRONLY,0);
的printf(%I \\ N(INT)FD); // 3
关闭(FD);
}
我知道 3
和 4
重新presents 0,1,2作为文件描述符分别为标准输入
,标准输出
和标准错误
。显然的fopen
不使用文件描述符。
这是什么 FILE *
重新present的价值?如何的fopen
如果不与文件描述符?
这是什么文件的价值*重新present?
块引用>这是一个指向
文件
结构,glibc的其定义为<一个href=\"https://sourceware.org/git/?p=glibc.git;a=blob;f=libio/libio.h;h=efd09f120b49a7cdeef11baba8bce8d4e94215db;hb=HEAD#l241\"相对=nofollow>此处。其中,除其他事项外,包含的文件描述符。你可以从FILE *
使用POSIX的的fileno
功能。有关详细信息,你可能会喜欢有进去一看文件描述符互动和标准I / O流。
I have this program
#include <stdio.h> #include <unistd.h> #include <sys/socket.h> #include <fcntl.h> int main(void) { FILE* f = fopen("/Users/user/a.cc", "rb"); printf("%i\n", f); // 1976385616 printf("%i\n", *f); // 1976385768 int sockfd = socket(AF_UNIX, SOCK_STREAM, 0); printf("%i\n", sockfd); // 4 fclose(f); close(sockfd); int fd = open("/Users/user/a.cc", O_TRUNC | O_WRONLY, 0); printf("%i\n", (int) fd); // 3 close(fd); }
I know that
3
and4
represents the file descriptors with 0, 1, 2 beingstdin
,stdout
andstderr
respectively. Obviouslyfopen
doesn't use a file descriptor.What does the value of
FILE*
represent? How doesfopen
if not with file descriptors?解决方案What does the value of FILE* represent?
It is a pointer to
FILE
structure, for glibc its definition is here. Which, among other things, contains the file descriptor. You can get the file descriptor fromFILE*
using POSIXfileno
function.For more details you may like having a look into Interaction of File Descriptors and Standard I/O Streams.
这篇关于什么是文件的内容psented重新$ P $ *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!