当前位置:网站首页>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);
}
运行结果:
边栏推荐
- How can I take a shortcut to learn C language in college
- 学习单片机对社会的帮助是很大的
- 五月集训总结——来自阿光
- Mapreduce实例(八):Map端join
- MapReduce instance (IX): reduce end join
- A wave of open source notebooks is coming
- CANoe不能自动识别串口号?那就封装个DLL让它必须行
- Interview shock 62: what are the precautions for group by?
- [deep learning] semantic segmentation: paper reading: (2021-12) mask2former
- Why is 51+ assembly in college SCM class? Why not come directly to STM32
猜你喜欢
What you have to know about network IO model
Why can't TN-C use 2p circuit breaker?
What are the models of data modeling
[Yu Yue education] reference materials of complex variable function and integral transformation of Shenyang University of Technology
零基础学习单片机切记这四点要求,少走弯路
Embedded development is much more difficult than MCU? Talk about SCM and embedded development and design experience
五月集训总结——来自阿光
Research and implementation of hospital management inpatient system based on b/s (attached: source code paper SQL file)
Elk project monitoring platform deployment + deployment of detailed use (II)
MapReduce instance (VIII): Map end join
随机推荐
面试突击62:group by 有哪些注意事项?
There are software load balancing and hardware load balancing. Which one to choose?
Control the operation of the test module through the panel in canoe (Advanced)
[one click] it only takes 30s to build a blog with one click - QT graphical tool
Mapreduce实例(五):二次排序
数据建模有哪些模型
小白带你重游Spark生态圈!
Some thoughts on the study of 51 single chip microcomputer
The real future of hardware engineers may not be believed by you if I say so
Cooperative development in embedded -- function pointer
[CV] target detection: derivation of common terms and map evaluation indicators
软件负载均衡和硬件负载均衡的选择
硬件工程师的真实前途我说出来可能你们不信
Interview shock 62: what are the precautions for group by?
Mapreduce实例(四):自然排序
CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?
CAP理论
Defensive C language programming in embedded development
CAPL 脚本对.ini 配置文件的高阶操作
嵌入式开发中的防御性C语言编程