linux c fopen open 互相转换 文件指针 到 文件描述符 FILE* 转 FD

FILE*--fileno()--->fd;

实例如下:

#include <stdio.h>

int main()
{
 FILE *fp = NULL;
 int i = 0;
 fp = fopen("test.txt", "w+");
 fclose(fp);
 while(i++<1500)
 {
  fp = fopen("test.txt", "a");  // "注意W+对 int fd = fileno(fp);的影响"
  if (fp == NULL)
   return 0;
  
  fprintf(fp, "hello%d\n",i);
  fflush(fp);
  
  int fd = fileno(fp); //如果是W+ 这里文件被清空的影响
  
  write(fd, "world\n", sizeof("world\n"));
  
  fclose(fp);  // close(fd);也行
  }
}

猜你喜欢

转载自blog.csdn.net/whatday/article/details/114916313