open函數相關的: /* open 是系統調用 返回的是文件句柄*/
#include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode);
fopen函數相關的: /* open是ANSIC標准中的C語言庫函數,在不同的系統中應該調用不同的內核api */
FILE *fopen(const char *path, const char *mode); FILE *fdopen(int fd, const char *mode); FILE *freopen(const char *path, const char *mode, FILE *stream);
函數說明:fileno()用來取得參數stream 指定的文件流所使用的文件描述詞.
void clearerr(FILE *stream); int feof(FILE *stream); int ferror(FILE *stream); int fileno(FILE *stream);
實例:
#include <stdio.h> int main(int argc, char **argv) { FILE * fp; int fd; fp = fopen("/etc/passwd", "r"); fd = fileno(fp); //等價於 //fd=open("etc/passwd",RD_ONLY); printf("fd=%d\n", fd); fclose(fp); return 0; }