当前位置:网站首页>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);
}运行结果:

边栏推荐
猜你喜欢

Interview shock 62: what are the precautions for group by?
![[one click] it only takes 30s to build a blog with one click - QT graphical tool](/img/f0/52e1ea33a5abfce24c4a33d107ea05.jpg)
[one click] it only takes 30s to build a blog with one click - QT graphical tool

机械工程师和电气工程师方向哪个前景比较好?

Hero League rotation map automatic rotation
![[deep learning] semantic segmentation: thesis reading (neurips 2021) maskformer: per pixel classification is not all you need](/img/84/cfcd006d445fc54ea0eda3f92e7d9d.jpg)
[deep learning] semantic segmentation: thesis reading (neurips 2021) maskformer: per pixel classification is not all you need

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

Redis distributed lock implementation redison 15 questions

51单片机进修的一些感悟

Research and implementation of hospital management inpatient system based on b/s (attached: source code paper SQL file)

Control the operation of the test module through the panel in canoe (primary)
随机推荐
Learning SCM is of great help to society
Hero League rotation chart manual rotation
112 pages of mathematical knowledge sorting! Machine learning - a review of fundamentals of mathematics pptx
MapReduce instance (V): secondary sorting
硬件工程师的真实前途我说出来可能你们不信
In order to get an offer, "I believe that hard work will make great achievements
Several silly built-in functions about relative path / absolute path operation in CAPL script
Cap theory
Interview shock 62: what are the precautions for group by?
Scoped in webrtc_ refptr
If a university wants to choose to study automation, what books can it read in advance?
英雄联盟轮播图自动轮播
Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)
How does the single chip microcomputer execute the main function from power on reset?
嵌入式开发比单片机要难很多?谈谈单片机和嵌入式开发设计经历
Nc29 search in two-dimensional array
Programmation défensive en langage C dans le développement intégré
CANoe的数据回放(Replay Block),还是要结合CAPL脚本才能说的明白
英雄联盟轮播图手动轮播
Combined search /dfs solution - leetcode daily question - number of 1020 enclaves