文件

简介: 在磁盘上所有的东西都是文件,c盘包括桌面的

文件

文件的详细介绍

引子image.png

目录

文件的详细介绍

什么是文件

文件名

文件指针

文件的使用

文件的打开与关闭

顺序读写(流的概念)

fputc

fgetc

fputs fgets

fprintf,fscanf,sscanf,sprintf

fwrite fread

文件的随机读取

fseek ftell rewind

文件读取结束的判定

feof ferror

文件缓冲区

image.png

image.png

1.0

#include<stdio.h>
#include<stdlib.h>
int main()
{
  FILE *pf=fopen("tst.dat", "w");*///"test.dat"是文件名,"w"是打开方式(以写的形式进行创建)(只写)“w”为了 输出数据,打开一个文件,如果指定文件不存在,就新建一个(在此文件中开辟)
     if (pf == NULL)
  {
    perror("fopen");
    return 1;
  }
  //写文件
  //关闭文件(不想要了)
  fclose(pf);
  pf = NULL;//可类比动态内存开辟,补不置成空指针就会报错
  return 0;
}

1.1

#include<stdio.h>
int main()
{
  FILE *pf=fopen("D:\\2021\\class\\test.7\\test.dat", "w");//如果文件并不是在此文件下创建的化,同样也不会运行成功,但使用绝对路径是可以的
  //,单斜杠情况下会将后面理解为转义字符,因此都要使用双斜杠,用于将\打印出来
  if (pf == NULL)
  {
    perror("fopen");
    return 1;
  }
  //写文件
  //关闭文件(不想要了)
  fclose(pf);
  pf = NULL;//
  return 0;
}

1.3

#include<stdio.h>
int main()
{
  FILE *pf = fopen("tst.dat", "r");//以读的方式打开
  if (pf == NULL)
  {
    perror("fopen");
    return 1;
  }
  //写文件
  //关闭文件(不想要了)
  fclose(pf);
  pf = NULL;//可类比动态内存开辟,
  return 0;
}

文件的顺序读写

image.png

#include<stdio.h>
int main()
{
  FILE *pf = fopen("test.dat", "w");//以w的形式打开,即使原来有内容也会清空掉,
  if (pf == NULL)
  {
    perror("fopen");
    return 1;
  }
  //写文件
  fputc('b', pf);//文件字符输入,
  fputc('i', pf);
  fputc('t', pf);//便会向test.dat文件中输入bit
  // 关闭文件
  fclose(pf);
  pf = NULL;
  return 0;
}

2.1

#include<stdio.h>
int main()
{
  fputc('c', stdout);//便在屏幕上打印了c这字符
  fputc('b', stdout);
  fputc('a', stdout);
  return 0;
}

image.png

#include<stdio.h>
//文本中输入abcdef
int main()
{
  FILE *pf = fopen("test.dat", "r");//以w的形式打开,即使原来有内容也会清空掉,
  if (pf == NULL)
  {
    perror("fopen");
    return 1;
  }
  //读文件,fgetc从文件首字符中一个一个读取
  //int ret=fgetc(pf);//从文件流里面读一个字符,可以从文件,也可以从标准输入流中,文件字符输入,如果读取正常,会返回此字符的ascll值,如果读取失败,会返回EOF(-1)
  //printf("%c", ret);
  //ret = fgetc(pf);
  //printf("%c", ret);//读取的都是test.dat
  int ret = fgetc(stdin);//从标准屏幕输入流里面读一个字符,可以从文件,也可以从标准输入流中,文件字符输入,如果读取正常,会返回此字符的ascll值,如果读取失败,会返回EOF(-1)
  printf("%c", ret);
  ret = fgetc(stdin);
  printf("%c", ret);//读取的都是test.dat,文件结束就会输出EOF为-1
  fclose(pf);
  pf = NULL;
  return 0;
}

image.png

#include<stdio.h>
int main()
{
  FILE*pf = fopen("test.dat", "w");//以写的形式打开
    if (pf == NULL)
    {
      perror("fopen");
      return 1;
    }
  //写文件-按行写
  fputs("abcdef\n",pf);//s-string写字符串打个\n就可以换行,pf是要写的文件流
  fputs("ghijk\n", pf);
  //用完关闭文件
  fclose(pf);
  pf = NULL;
  return 0;
}
#include<stdio.h>
int main()
{
  char arr[10] = { 0 };
  FILE*pf = fopen("test.dat", "r");//以读的形式打开
  if (pf == NULL)
  {
    perror("fopen");
    return 1;
  }
  //读文件-按行读
  fgets(arr, 4, pf);//fgets(const char*string,const int n,const stream),n是可以读的最大字符,但多了一个\0n要+1
  printf("%s\n", arr);//读完从c后开始读
  fgets(arr, 4, pf);//最多读3个
  printf("%s\n", arr);
  //用完关闭文件
  fclose(pf);
  pf = NULL;
  return 0;
}

fprintf,fscanf,sscanf,sprintf

#include<stdio.h
struct s
{
  char age[10];
  int a;
  float f;
};
int main()
{
  struct s a = { "hello",20,5.5f };
  char buf[100] = { 0 };
  struct s tmp = { 0 };
  //sprintf是把格式化的数据转化成字符串,
  sprintf(buf, "%s %d %f", a.age, a.a, a.f);
  printf("%s\n", buf);
  //从buf这个字符串中还原出一个结构体
  //从字符串中读出格式化的数据
  sscanf(buf, "%s %d %f", tmp.age, &tmp.a, &tmp.f);
  printf("%s %d %f", tmp.age, tmp.a, tmp.f);
  return 0;
 }

image.png

struct a
{
  char arr[10];
  int num;
  float sc;
};
int main()
{
  struct a s = { "abcd",10,5.5f };
  FILE* pf = fopen("test.dat", "w");
  if (pf == NULL)
  {
    perror("fopen");
    return 1;
  }
  fprintf(pf, "%s %d %f", s.arr, s.num, s.sc);//格式化输出,写入pf文件流,
  return 0;
}

image.png

int main()
{
  struct a s = { "abcd",10,5.5 };
  FILE* pf = fopen("test.dat", "r");
  if (pf == NULL)
  {
    perror("fopen");
    return 1;
  }
  fscanf(pf, "%s %d %f", s.arr, &(s.num), &(s.sc));//格式化输出,写入pf文件流
  printf("%s %d %f", s.arr, s.num, s.sc);
  return 0;
}

image.png

int main()
{
  struct a s = { "abcd",10,5.5 };
  FILE* pf = fopen("test.dat", "w");
  if (pf == NULL)
  {
    perror("fopen");
    return 1;
  }
  fwrite(&s, sizeof(struct a), 1, pf);//二进制输出,写入pf文件流
  fclose(pf);
  pf = NULL;
  return 0;
}

image.png

int main()
{
  struct a s = { "abcd",10,5.5 };
  FILE* pf = fopen("test.dat", "r");
  if (pf == NULL)
  {
    perror("fopen");
    return 1;
  }
  fread(&s, sizeof(struct a), 1, pf);//二进制输入,读出pf文件流
  printf("%s %d %f", s.arr, s.num, s.sc);
  fclose(pf);
  pf = NULL;
  return 0;
}

image.png

int main()
{ 
  FILE *pf=fopen("test.dat", "r");
  if (pf == NULL)
  {
    perror("fopen");
  }
  //顺序读写,从头开始
  int ch = fgetc(pf);
  printf("%c", ch);
  fseek(pf, 2, SEEK_CUR);//偏移的单位是字节,从当前位置向后偏移两个字节
    ch = fgetc(pf);
    printf("%c", ch);
  ch = fgetc(pf);
  printf("%c", ch);
  int ret = ftell(pf);
  printf("%d", ret);
  //让文件指针回到起始位置用rewind()
  rewind(pf);//回到起始位置
  int d = ftell(pf);
  printf("%d", d);
  return 0;
} 

image.png

int main()
{
  int a = 10000;//00002710小端存储会是10 27 00 00
  FILE* pf = fopen("test.dat", "wb");
  if (pf == NULL)
  {
    perror("fopen");
  }
  //
  fwrite(&a, sizeof(int), 1, pf);
  fclose(pf);
  pf = NULL;
  return 0;
}

image.png

int main()
{
  FILE *pfd = fopen("text.txt", "r");
  if (pfd == NULL)
  {
    return 1;
  }
  FILE *pw = fopen("text2.txt", "w");
  if (pw == NULL)
  {
    fclose(pfd);
    pfd = NULL;
    return 1;//把第一个文件关掉,当第一个文件返回失败才会
  }
  //文件打开成功
  //读写文件
  int ch = 0;
  while ((ch = fgetc(pfd)) != EOF)//一个字符一个字符的输入直到eof
  {
    //写文件
    fputc(ch, pw);
    pw = NULL;
  }
  if (feof(pfd))
  {
    printf("遇到文件结束标志,文件正常结束");
  }
  else if (ferror(pfd))//判断是否遇到错误
  {
    printf("文件读取失败结束");
  }
  fclose(pfd);
pfd=NULL;
fclose(pw)
pw=NULL;
  return 0;
}
enum { size = 5 };
int main()
{
  double a[size] = { 1,2,3,4,5 };
  FILE *pf = fopen("test.dat", "wb");//必须用二进制模式
  fwrite(a, sizeof *a, size, pf);//写double的数组
  fclose(pf);
  double b[size];
  pf = fopen("test.dat", "rb");
  size_t ret_code = fread(b, sizeof*b, size, pf);//读double的数组
  if (ret_code == size)
  {
    puts("arry read succseefully,contents");
    for (int n = 0; n < size; n++)
    {
      printf("%f", b[n]);
      putchar('\n');
    }
  }
  else
  {
    if (feof(pf))
    {
      printf("error reading test.bin:unexpected end of file");
    }
    else if (ferror(pf))
    {
      perror("error reading test.bin");
    }
  }
  return 0;
  }

image.png

#include<windows.h>
int main()
{
  FILE *pf = fopen("test.dat", "w");
  fputs("abcde", pf);
  printf("睡眠10秒-已经写数据,打开test.txt文件,文件没有内容\n");
  sleep(10000);
  printf("刷新缓冲区\n");
  fflush(pf);//刷新缓冲区,才将输出缓冲区的数据写到文件(磁盘)
    //fflush在高端vs上不能使用
  printf("再睡眠10秒,打开test.txt文件,有内容了");
  sleep(10000);
  //缓冲区里的数据
  fclose(pf);
  pf = NULL;
  return 0;
}

image.png

相关文章
|
2月前
|
存储 内存技术
什么是文件
什么是文件
15 0
|
5月前
文件
文件操作。
17 0
|
8月前
|
NoSQL Python
PythonExcel文件
在Python中,我们可以使用许多库来处理Excel文件,其中最常用的是pandas和openpyxl。
|
11月前
|
数据安全/隐私保护
p文件如何转为m文件
如果你是MATLAB爱好者,你一定知道MATLAB P文件,但是,你是否也和许多人一样,摸不着头脑?难道你也经常遇到MATLAB P文件加密问题,难以阅读和研究吗?那么,你需要一个MATLAB P文件解密工具!
|
编译器 数据库 C语言
文件(上)——“C”
文件(上)——“C”
|
存储 C语言
文件(下)——“C”
文件(下)——“C”
|
存储
Fasta和Fastq文件
Fasta和Fastq文件
178 0
小技巧——对比两个文件是否相同
小技巧——对比两个文件是否相同
81 0
文件的使用详解
👩‍💻博客主页:[风起 风落](https://blog.csdn.net/qq_62939852?spm=1001.2101.3001.5343)的博客主页 ✨欢迎关注🖱点赞🎀收藏⭐留言✒ 👕参考网站:牛客网 💻首发时间:🎞2022年7月30日🎠 🎨你的收入跟你的不可替代成正比 🀄如果觉得博主的文章还不错的话,请三连支持一下博主哦 💬给大家介绍一个求职刷题收割offer的地方👉[点击网站](https://www.nowcoder.com/link/pc_csdncpt_fqfl_c)
109 0
文件的使用详解