linux c之fdopen(int fd, const char *type)使用总结

来源:互联网 发布:知敬畏守底线强党性 编辑:程序博客网 时间:2024/06/18 07:07

1、fdopen(int fd, const char *type)的介绍

比如一写特殊文件不能用io打开,我们先要用open函数得到文件描述符,也就是这个fdopen函数的第一个参数,第二个参数是常量,不同类型不同意义,如下图


2、代码演示

#include<stdio.h>#include<fcntl.h>int main(void){   FILE *fp;   int fd;   if ((fp = fopen("hello.txt", "w+")) == NULL) {       printf("fopen file error\n");       return 0;   }   fprintf(fp, "hello word\n");   fclose(fp);   if ((fd = open("hello.txt", O_RDWR)) == -1) {       printf("open file fail\n");       return 0;   }   if ((fp = fdopen(fd, "a+")) == NULL) {       printf("fdopen open\n");       return 0;   }   fprintf(fp, "linux c program");   fclose(fp);   return 0;} 



3、结果展示



0 0
原创粉丝点击