欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-29 08:12:41
```c #include int main(int argc, char* argv[]) { // 创建文件类型 FILE* file; char buf[1024] = {0, }; // a 是追加,+ 文件不存在可以进行创建 file = fopen("1.txt", "a+"); // ... ......
#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;
}