0
点赞
收藏
分享

微信扫一扫

(每日一练c语言)写入字符串到文件txt

Just_Esme 2022-02-01 阅读 72

写入字符串到文件

下面正确将"Hello,World!"写入文件,并再次打开读取出来的代码是

#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", "r+");
  char buff[1024];
  fgets(buff, 1024, f2);
  fclose(f2);

  // 打印
  printf("%s", buff);

  return 0;
}
举报

相关推荐

0 条评论