使用C语言实现文件的操作

#include <stdio.h>

int main(int argc, char* argv[])
{
  // 创建文件类型
  FILE* file;
  char buf[1024] = {0, };
  //  a 是追加,+ 文件不存在可以进行创建
  file = fopen("1.txt", "a+");
  // 写入到文件 内容是hello world, 每一个字符大小是1,一共有13个字符
  fwrite("hello world", 1, 13, file);
  
  // 移动游标到文件开头
  rewind(file);
  
  // 读文件,到buf里面
  fread(buf, 1, 26, file);
  printf("buf: %s\n", buf);
  
  // 关闭文件
  fclose(file);
  return 0;
}

猜你喜欢

转载自www.cnblogs.com/fandx/p/12122878.html