什么是文件的内容psented重新$ P $ * [英] What is represented by the content of FILE*

查看:207
本文介绍了什么是文件的内容psented重新$ P $ *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个计划。

 的#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 and 4 represents the file descriptors with 0, 1, 2 being stdin, stdout and stderr respectively. Obviously fopen doesn't use a file descriptor.

What does the value of FILE* represent? How does fopen 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 from FILE* using POSIX fileno function.

For more details you may like having a look into Interaction of File Descriptors and Standard I/O Streams.

这篇关于什么是文件的内容psented重新$ P $ *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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