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.