File pointer/handle (File *), file descriptor (FD), and file path (filepath) Conversion

Source: Internet
Author: User

From http://blog.csdn.net/jenghau/article/details/5532265

FILE pointer/handle (FILE *), FILE descriptor (fd), and FILE path (filepath) Conversion

 

Recently, programming in linux often requires operations on some files. Sometimes, you need to convert the FILE pointer/handle (FILE *), FILE descriptor (fd), and FILE path (filepath) to meet the actual programming needs.

Now we can simply sort it out. As follows.

 

 

1: The file path file descriptor should be unique. The file pointer (value) is not unique, but the object to which it points should also be unique.
2: FILE * contains fd information and IO buffer. Therefore, it can be understood that FILE * is an encapsulation of fd and a standard form of C, therefore, FILE * is more suitable for cross-platform use than fd, and fopen should be used more than open.

 

3: Conversion

FILE Path to FILE pointer: filepath -- fopen () --> FILE *;
File Path to file descriptor: filepath -- open () -- fd;

 

FILE descriptor to FILE pointer: fd -- fdopen () --> FILE *;
File descriptor to file path: fd -- readlink (/proc/% getpid ()/fd/% fd ") --> filepath; // This is a" curve saving"

FILE pointer to FILE descriptor: FILE * -- fileno () ---> fd;
FILE pointer to FILE path: FILE *---??? --- PATH; // The direct conversion method is not found yet. Please add.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.