我有这个程序
#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); }
我知道3
和4
代表0,1,2为stdin
, stdout
和stderr
文件描述符。 fopen
显然不使用文件描述符。
FILE*
的值代表什么? 如果不使用文件描述符, fopen
如何?
FILE *的值代表什么?
它是一个指向FILE
结构的指针,对于glibc,它的定义就在这里 。 其中包含文件描述符。 您可以使用POSIX fileno
函数从FILE*
获取文件描述符。
有关更多详细信息,可能需要查看文件描述符和标准I / O流的交互 。