当前位置: 首页 > news >正文

文件(下)——“C”

各位CSDN的uu们你们好呀,今天,小雅兰的内容是文件的知识点,下面,就让我们进入文件的世界吧


文件的顺序读写

文件的随机读写

                fseek

                ftell

                rewind

文本文件和二进制文件

文件读取结束的判定

文件缓冲区


 在上篇博客中,最后,其实我写了关于流的知识点,但是不全面,需要补充一些内容,我们一起来复习一下:

文件(上)——“C”_认真学习的小雅兰.的博客-CSDN博客

文件的顺序读写 

流:

stdin FILE*
stdout FILE*
stderr FILE* 

 举个例子:

#define _CRT_SECURE_NO_WARNINGS 1
//stdin FILE*
//stdout FILE*
//stderr FILE*
#include<stdio.h>
int main()
{int ch = fgetc(stdin);printf("%c\n", ch);return 0;
}

#define _CRT_SECURE_NO_WARNINGS 1
//stdin FILE*
//stdout FILE*
//stderr FILE*
#include<stdio.h>
int main()
{fputc('a', stdout);fputc('b', stdout);fputc('c', stdout);fputc('d', stdout);return 0;
}

对比一组函数:

 scanf/fscanf/sscanf

 printf/fprintf/sprintf

 

  

 

#include<stdio.h>
struct S
{int n;float f;char arr[20];
};
int main()
{struct S s = { 520,3.5f,"wangwu" };//把一个结构体转化成字符串char arr[200] = { 0 };sprintf(arr, "%d %f %s\n", s.n, s.f, s.arr);printf("%s\n", arr);return 0;
}

 

 

#include<stdio.h>
struct S
{int n;float f;char arr[20];
};
int main()
{struct S s = { 520,3.5f,"wangwu" };//把一个结构体转化成字符串char arr[200] = { 0 };sprintf(arr, "%d %f %s\n", s.n, s.f, s.arr);printf("字符串的数据:%s\n", arr);//把一个字符串转换成对应的格式化数据struct S tmp = { 0 };sscanf(arr, "%d %f %s\n", &(tmp.n),&(tmp.f), tmp.arr);printf("格式化的数据:%d %f %s\n", tmp.n, tmp.f, tmp.arr);return 0;
}

 

二进制:

  

 

#include<stdio.h>
struct S
{char name[20];int age;float score;
};
int main()
{struct S s = { "zhangsan",20,95.5f };FILE* pf = fopen("C:\\Users\\86137\\Desktop\\sss.txt.txt", "wb");if (pf == NULL){perror("fopen");return 1;}//写文件fwrite(&s,sizeof(struct S),1,pf);//关闭文件fclose(pf);pf = NULL;return 0;
}

 

这样的原因就是:是以二进制的形式写出来的 

 

 

 

#include<stdio.h>
struct S
{char name[20];int age;float score;
};
int main()
{struct S s = { 0 };FILE* pf = fopen("C:\\Users\\86137\\Desktop\\sss.txt.txt", "rb");if (pf == NULL){perror("fopen");return 1;}//读文件fread(&s, sizeof(struct S), 1, pf);printf("%s %d %f\n", s.name, s.age, s.score);//关闭文件fclose(pf);pf = NULL;return 0;
}

 


文件的随机读写

fseek

 

                 根据文件指针的位置和偏移量来定位文件指针。

                int fseek ( FILE * stream, long int offset, int origin );

 

下面,来使用一下该函数:

 

#include<stdio.h>
int main()
{//打开文件FILE* pf = fopen("C:\\Users\\86137\\Desktop\\sss.txt.txt", "r");if (pf == NULL){perror("fopen");return 1;}//读文件int ch = fgetc(pf);printf("%c\n", ch);//ach = fgetc(pf);printf("%c\n", ch);//bch = fgetc(pf);printf("%c\n", ch);//cch = fgetc(pf);printf("%c\n", ch);//dfseek(pf, -3, SEEK_CUR);ch = fgetc(pf);printf("%c\n", ch);//希望读到的是b//关闭文件fclose(pf);pf = NULL;return 0;
}

 

 

ftell

 

 

                返回文件指针相对于起始位置的偏移量

                 long int ftell ( FILE * stream );

下面,还是来使用一下该函数: 

 

#include<stdio.h>
int main()
{//打开文件FILE* pf = fopen("C:\\Users\\86137\\Desktop\\sss.txt.txt", "r");if (pf == NULL){perror("fopen");return 1;}//读文件int ch = fgetc(pf);printf("%c\n", ch);//ach = fgetc(pf);printf("%c\n", ch);//bch = fgetc(pf);printf("%c\n", ch);//cch = fgetc(pf);printf("%c\n", ch);//dprintf("%d\n", ftell(pf));//关闭文件fclose(pf);pf = NULL;return 0;
}

 

rewind

 

 

                让文件指针的位置回到文件的起始位置  

                void rewind ( FILE * stream );

 下面,还是来使用一下该函数: 

#include<stdio.h>
int main()
{//打开文件FILE* pf = fopen("C:\\Users\\86137\\Desktop\\sss.txt.txt", "r");if (pf == NULL){perror("fopen");return 1;}//读文件int ch = fgetc(pf);printf("%c\n", ch);//ach = fgetc(pf);printf("%c\n", ch);//bch = fgetc(pf);printf("%c\n", ch);//cch = fgetc(pf);printf("%c\n", ch);//drewind(pf);printf("%d\n", ftell(pf));ch = fgetc(pf);printf("%c\n", ch);//关闭文件fclose(pf);pf = NULL;return 0;
}

 


 文本文件和二进制文件

根据数据的组织形式,数据文件被称为文本文件或者二进制文件

数据在内存中以二进制的形式存储,如果不加转换的输出到外存,就是二进制文件。

如果要求在外存上以ASCII码的形式存储,则需要在存储前转换。以ASCII字符的形式存储的文件就是文本文件。

一个数据在内存中是怎么存储的呢?

字符一律以ASCII形式存储,数值型数据既可以用ASCII形式存储,也可以使用二进制形式存储。

如有整数10000,如果以ASCII码的形式输出到磁盘,则磁盘中占用5个字节(每个字符一个字节),而二进制形式输出,则在磁盘上只占4个字节(VS2013测试)。

 

#include <stdio.h>
int main()
{int a = 10000;FILE* pf = fopen("test.txt", "wb");fwrite(&a, 4, 1, pf);//二进制的形式写到文件中fclose(pf);pf = NULL;return 0;
}

 

 


文件读取结束的判定

被错误使用的feof

牢记:在文件读取过程中,不能用feof函数的返回值直接用来判断文件的是否结束

feof的作用是:当文件读取结束的时候,判断是读取结束的原因是否是:遇到文件尾结束。

文本文件读取是否结束,判断返回值是否为 EOF ( fgetc ),或者 NULL ( fgets )

  • fgetc 判断是否为 EOF .
  • fgets 判断返回值是否为 NULL .

  

二进制文件的读取结束判断,判断返回值是否小于实际要读的个数。

  •  fread判断返回值是否小于实际要读的个数。

正确的使用:

文本文件的例子:

#include <stdio.h>
#include <stdlib.h>
int main()
{int c; // 注意:int,非char,要求处理EOFFILE* fp = fopen("test.txt", "r");if (!fp) {perror("File opening failed");return EXIT_FAILURE;}//fgetc 当读取失败的时候或者遇到文件结束的时候,都会返回EOFwhile ((c = fgetc(fp)) != EOF) // 标准C I/O读取文件循环{putchar(c);}//判断是什么原因结束的if (ferror(fp))puts("I/O error when reading");else if (feof(fp))puts("End of file reached successfully");fclose(fp);
}

二进制文件的例子:

#include <stdio.h>
enum { SIZE = 5 };
int main(void)
{double a[SIZE] = { 1.,2.,3.,4.,5. };FILE* fp = fopen("test.bin", "wb"); // 必须用二进制模式fwrite(a, sizeof * a, SIZE, fp); // 写 double 的数组fclose(fp);double b[SIZE];fp = fopen("test.bin", "rb");size_t ret_code = fread(b, sizeof * b, SIZE, fp); // 读 double 的数组if (ret_code == SIZE) {puts("Array read successfully, contents: ");for (int n = 0; n < SIZE; ++n) printf("%f ", b[n]);putchar('\n');}else{ // error handlingif (feof(fp))printf("Error reading test.bin: unexpected end of file\n");else if (ferror(fp)) {perror("Error reading test.bin");}}fclose(fp);
}

文件缓冲区

ANSIC 标准采用“缓冲文件系统”处理的数据文件的,所谓缓冲文件系统是指系统自动地在内存中为程序 中每一个正在使用的文件开辟一块“文件缓冲区”。

从内存向磁盘输出数据会先送到内存中的缓冲区,装满缓冲区后才一起送到磁盘上。如果从磁盘向计算机读入数据,则从磁盘文件中读取数据输入到内存缓冲区(充满缓冲区),然后再从缓冲区逐个地将数据送到程序数据区(程序变量等)。缓冲区的大小根据C编译系统决定的。

 

证明缓冲区的存在: 

#include <stdio.h>
#include <windows.h>
//VS2013 WIN10环境测试
int main()
{FILE* pf = fopen("test.txt", "w");fputs("abcdef", pf);//先将代码放在输出缓冲区printf("睡眠10秒-已经写数据了,打开test.txt文件,发现文件没有内容\n");Sleep(10000);printf("刷新缓冲区\n");fflush(pf);//刷新缓冲区时,才将输出缓冲区的数据写到文件(磁盘)//注:fflush 在高版本的VS上不能使用了printf("再睡眠10秒-此时,再次打开test.txt文件,文件有内容了\n");Sleep(10000);fclose(pf);//注:fclose在关闭文件的时候,也会刷新缓冲区pf = NULL;return 0;
}

 这里可以得出一个结论:

因为有缓冲区的存在,C语言在操作文件的时候,需要做刷新缓冲区或者在文件操作结束的时候关闭文件。 如果不做,可能导致读写文件的问题。


实现一个代码,拷贝一个文件

#include <stdio.h>
int main()
{//打开文件//打开被读的文件FILE* pfRead = fopen("test1.txt", "r");if (pfRead == NULL){perror("open file for read");return 1;}//打开要写的文件FILE* pfWrite = fopen("test2.txt", "w");if (pfWrite == NULL){fclose(pfRead);pfRead = NULL;pfRead = NULL;perror("open file for write");return 1;}//拷贝int ch = 0;while (ch = fgetc(pfRead) != EOF){fputc(ch, pfWrite);}//关闭文件fclose(pfRead);pfRead = NULL;fclose(pfWrite);pfWrite = NULL;return 0;
}

好啦,小雅兰的文件知识点到这里就完完整整结束啦,还要继续加油噢!!!

 

http://www.lryc.cn/news/45588.html

相关文章:

  • bugku 渗透靶场3
  • NER 任务以及联合提槽任务
  • scala函数式编程
  • 网吧2023:热闹回来了,电竞战歌起
  • 代码随想录算法训练营第五十九天|503.下一个更大元素II、42. 接雨水
  • 9、简单功能分析
  • 如何发送和接收参数?五种参数传递方法
  • 蓝桥杯C/C++VIP试题每日一练之矩形面积交
  • Spark大数据处理讲课笔记2.4 IDEA开发词频统计项目
  • 【ChatGPT 】国内无需注册 openai 即可访问 ChatGPT:ChatGPT Sidebar 浏览器扩展程序的安装与使用
  • 使用fetch()异步请求API数据实现汇率转换器
  • GPT-4“王炸”,10秒钟开发一套Web + APP 系统
  • Disjoint 集合数据结构或 Union-Find 算法简介
  • uniapp中nvue与vue的区别?
  • 带头双向循环链表的实现
  • 大屏使用dv-digital-flop定时刷新显示总人数
  • Java面向对象部分 个人学习记录
  • MySQL数据库——对Linux MySQL软件包的一些说明
  • 【JavaEE进阶】——第二节.Spring核心和设计思想
  • twitter开源算法(1)For You推荐系统架构
  • A General Framework for Uncertainty Estimation in Deep Learning源码阅读(二)
  • 串行通信协议---HART协议
  • 【独家】华为OD机试 - 寻找密码(C 语言解题)
  • FPGA有哪些优质的带源码的IP开源网站?
  • 基于模型预测控制(MPC)的微电网调度优化的研究(Matlab代码实现)
  • Postman接口测试之Mock快速入门
  • 分享一个国内可用的免费ChatGPT网站
  • 15. 三数之和(Java)
  • Navicat Premium 16安装教程
  • 蓝桥杯刷题冲刺 | 倒计时8天