fopen 與 open可以轉換 即 FILE-->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);也行
  }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章