#include <stdio.h>

int main(int argc, char **args) {
  // 写入
  FILE *f1 = fopen("/tmp/hello.txt", "w+");
  fputs("Hello,World!\n", f1);
  fclose(f1);

  // 追加
  FILE *f2 = fopen("/tmp/hello.txt", "a+");
  fputs("Hello,World!\n", f1);
  fclose(f2);

  // 读取
  FILE *f3 = fopen("/tmp/hello.txt", "r+");
  char buff[1024];

  fgets(buff, 1024, f3);
  printf("%s", buff);

  fgets(buff, 1024, f3);
  printf("%s", buff);

  fclose(f3);

  return 0;
}

更多推荐

C语言文件追加及写入及读取