当前位置:网站首页>C杂讲 文件 初讲
C杂讲 文件 初讲
2022-07-06 09:04:00 【Bright-SKY】
目录
5、文件块的读写fread fwrite 内存数据原样 输出到磁盘
知识点1【文件的概述】
1、文件的分类
磁盘文件:文件用来存放程序、文档、音频、视频数据、图片等数据的。
设备文件:在操作系统中把每一个与主机相连的输入、输出设备看作是一个文件,把它们的输入、输出等同于对磁盘文 件的读和写

(缓冲区的目的:提高存取效率 磁盘使用寿命)
2、磁盘文件的分类
物理上 所有的磁盘文件都是 二进制存储,以字节为单位 顺序存储。
逻辑上 文件分类:
文本文件:基于字符编码的文件
二进制文件:基于值编码的文件


总结:

3、文件指针


注意:初学不要在意FILE的细节 只需要 会用FILE 定义指针变量就行:FILE *fp=NULL;

知识点2【操作文件的库函数】
1、fopen打开文件
#include <stdio.h>
FILE *fopen(const char *path, const char *mode);mode:
读写权限:r读 w写 a追加 +可读可写 t文本文件(省略) b二进制文件
r:以只读方式打开文件
文件不存在返回 NULL;
文件存在返回文件指针,进行后续的读操作
w:以只写方式打开文件
文件不存在,以指定文件名创建此文件,并且打开文件;
若文件存在,清空文件内容,打开文件,然后进行写操作;
如果文件打不开(比如文件只读),返回 NULL
a:以追加方式打开文件
文件不存在,以指定文件名创建此文件(同 w)
若文件存在,从文件的结尾处进行写操作


2、关闭文件 fclose
#include <stdio.h>
int fclose(FILE *fp);
3、一次读写一个字符
(1)、字节的读操作 fgetc
int fgetc(FILE *stream)//读操作返回值:
以 t 的方式: 读到文件结尾返回 EOF
以 b 的方式:读到文件结尾,使用 feof(后面会讲)

void test03()
{
char buf[128]="";
int i=0;
FILE *fp = NULL;
//1、使用fopen打开一个文件 获得文件指针
fp = fopen("a.txt", "r");
if(fp == NULL)
{
perror("fopen");
return;
}
//2、对文件的操作 fgetc
while(1)
{
//fgetc调用一次 读取到一个字节
buf[i] = fgetc(fp);
if(buf[i] == EOF)//EOF表已经对到文件末尾
{
break;
}
i++;
}
printf("buf=%s\n", buf);
//3、关闭文件
fclose(fp);
}运行结果:事先 本地 创建a.txt 文件内容 为 hello file

(2)、字节的写操作 fputc
int fputc(int c, FILE *stream)//写操作
fputc 将 c 的值写到 stream返回值:
如果输出成功,则返回输出的字节值;
如果输出失败,则返回一个 EOF
EOF 是在 stdio.h 文件中定义的符号

void test04()
{
char buf[128]="";
int i=0;
FILE * fp =NULL;
fp = fopen("b.txt", "w");
if(fp == NULL)
{
perror("fopen");
return;
}
//使用fputc进行文件的数据写入
printf("请输入要写入文件的字符串:");
fgets(buf,sizeof(buf),stdin);//会获取换行符
buf[strlen(buf)-1] = 0;//去掉键盘输入的换行符
//将字符串buf中的元素 逐个写入文件中
while(buf[i] != '\0')
{
fputc(buf[i], fp);
i++;
}
fclose(fp);
}运行结果:

例程:从一个文件(文本文件)中读取所有信息,写入另一个文件中
void test05()
{
//需求:从a.txt读取文件内容 写入到b.txt
FILE *fp1 =NULL;
FILE *fp2 =NULL;
//以只读的方式打开a.txt
fp1 = fopen("a.txt","r");
if(fp1 == NULL)
{
perror("fopen");
return;
}
//以只写的方式打开b.txt
fp2 = fopen("b.txt","w");
if(fp2 == NULL)
{
perror("fopen");
return;
}
//从fp1中 每读取一个 字节 写入到fp2中
while(1)
{
char ch;
//读
ch = fgetc(fp1);
if(ch == EOF)//已经读到文件末尾
break;
//写
fputc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
return;
}4、一次读写一个字符串
(1)、使用fgets从文件中获取字符串
char *fgets(char *s, int size, FILE *stream)从 stream 所指的文件中读取字符,在读取的时候碰到换行符或者是碰到文件的末尾停止读取。
或者是读取了 size-1 个字节停止读取,在读取的内容后面会加一个\0,作为字符串的结尾
返回值:
成功: 返回读到字符串的首元素地址
失败:返回NULL

获取文件一行的数据:
void test07()
{
char buf[128]="";
FILE *fp = NULL;
char *path = "c.txt";
fp = fopen(path, "r");
if(fp == NULL)
{
perror("fopen");
return;
}
//err 打开一个文件名叫"path" 而不是path指向的文件名"c.txt"
//fp = fopen("path", "r");
//fp = fopen("c.txt", "r");
while(fgets(buf,sizeof(buf),fp))
{
printf("%s\n", buf);
}
fclose(fp);
}运行结果:

(2)、使用fputs 往文件中写入一个字符串
int fputs(const char *s, FILE *stream); 
案例:
void test06()
{
//指针数组
char *buf[]={"窗前明月光\n","疑似地上霜\n","举头望明月\n","低头思故乡"};
int n = sizeof(buf)/sizeof(buf[0]);
FILE *fp = NULL;
int i=0;
fp = fopen("c.txt", "w");
if(fp == NULL)
{
perror("fopen");
return;
}
for(i=0;i<n; i++)
{
fputs(buf[i], fp);
}
fclose(fp);
}运行结果:

5、文件块的读写fread fwrite 内存数据原样 输出到磁盘
1、使用fwrite 将 数据块 写入到文件中

typedef struct
{
char name[16];//姓名
int deff;//防御
int atk;//攻击
}HERO;
void test08()
{
HERO hero[]={
{"德玛西亚",80, 60},
{"盲僧",90, 80},
{"小法",40, 85},
{"小炮",50, 90}
};
int n = sizeof(hero)/sizeof(hero[0]);
FILE *fp = NULL;
fp = fopen("hero.txt", "w");
if(fp == NULL)
{
perror("fopen");
return;
}
//fwrite 将内存的数据原样的 输出到 文件中
//写入文件的数据 不便于 用户查看 但是 不会影响 程序的读
fwrite(hero, sizeof(HERO), n, fp);
fclose(fp);
}运行结果:

2、使用fread 从文件中 读取 数据块

返回的是整块数 不足一块 不算的

案例:
void test09()
{
HERO hero[4];
int i=0;
FILE *fp = NULL;
fp = fopen("hero.txt", "r");
if(fp == NULL)
{
perror("fopen");
return;
}
fread(hero,sizeof(HERO), 4, fp);
for(i=0;i<4;i++)
{
//printf("英雄姓名:《%s》,防御:《%d》,伤害:《%d》\n", \
hero[i].name,hero[i].deff,hero[i].atk);
printf("英雄姓名:《%s》,防御:《%d》,伤害:《%d》\n", \
(hero+i)->name,(hero+i)->deff,(hero+i)->atk);
}
fclose(fp);
}运行结果:

边栏推荐
- CANoe仿真功能之自动化序列(Automation Sequences )
- Embedded development is much more difficult than MCU? Talk about SCM and embedded development and design experience
- [one click] it only takes 30s to build a blog with one click - QT graphical tool
- Scoped in webrtc_ refptr
- Leetcode:608 tree node
- 068.查找插入位置--二分查找
- Popularization of security knowledge - twelve moves to protect mobile phones from network attacks
- 在CANoe中通過Panel面板控制Test Module 運行(初級)
- 五月刷题27——图
- CANoe CAPL文件操作目录合集
猜你喜欢

Combined search /dfs solution - leetcode daily question - number of 1020 enclaves

CANoe仿真功能之自动化序列(Automation Sequences )

Segmentation sémantique de l'apprentissage profond - résumé du code source

Regular expressions are actually very simple
![[NLP] bert4vec: a sentence vector generation tool based on pre training](/img/fd/8e5e1577b4a6ccc06e29350a1113ed.jpg)
[NLP] bert4vec: a sentence vector generation tool based on pre training

Hugo blog graphical writing tool -- QT practice

大学C语言入门到底怎么学才可以走捷径

Canoe cannot automatically identify serial port number? Then encapsulate a DLL so that it must work

听哥一句劝,按这套嵌入式的课程内容和课程体系去学习

Detailed explanation of cookies and sessions
随机推荐
《ASP.NET Core 6框架揭秘》样章发布[200页/5章]
What are the models of data modeling
Mapreduce实例(九):Reduce端join
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
C#/. Net phase VI 01C Foundation_ 01: running environment, process of creating new C program, strict case sensitivity, meaning of class library
【深度学习】语义分割:论文阅读(NeurIPS 2021)MaskFormer: per-pixel classification is not all you need
为什么要数据分层
112 pages of mathematical knowledge sorting! Machine learning - a review of fundamentals of mathematics pptx
Popularization of security knowledge - twelve moves to protect mobile phones from network attacks
CAPL脚本中关于相对路径/绝对路径操作的几个傻傻分不清的内置函数
手把手教您怎么编写第一个单片机程序
The replay block of canoe still needs to be combined with CAPL script to make it clear
[Yu Yue education] reference materials of complex variable function and integral transformation of Shenyang University of Technology
[deep learning] semantic segmentation: paper reading: (CVPR 2022) mpvit (cnn+transformer): multipath visual transformer for dense prediction
【深度學習】語義分割-源代碼匯總
【深度学习】语义分割-源代码汇总
In order to get an offer, "I believe that hard work will make great achievements
[Chongqing Guangdong education] reference materials for nine lectures on the essence of Marxist Philosophy in Wuhan University
Mapreduce实例(七):单表join
嵌入式中的合作开发--函数指针