当前位置:网站首页>文件基础知识
文件基础知识
2022-07-25 18:06:00 【刀剑侠客】
文件有关知识
文件的分类
- 程序文件:像
.c为后缀的源文件,.obj为后缀的目标文件,.exe为后缀的可执行程序。 - 数据文件:程序读写时的数据。
或者按照另外一种分类方式
文件分为:1. 文本文件。2. 二进制文件。
数据在内存中就是以二进制的形式储存的,不加转换直接输出到外存中,就是二进制文件;要是加了转换,以ASCII标准存储就是文本文件。
数据文件
文件名:一个文件所拥有的唯一的标识。
包括:文件路径,文件名主干,文件后缀
文件的打开与关闭
- 文件指针
文件指针其实是一个结构体变量名FILE,里面的成员是用来存放文件的相关信息的。这些信息共同组成了每个不同文件的文件信息区。
每次打开文件的时候,系统会创建不同的FILE,通过FILE*创建一个变量pf,这里的pf就是一个文件指针,能够指向对应的文件。
- 文件的打开及关闭
文件在进行读写操作前,应该打开文件;当有关操作结束之后,应该关闭文件。
标准规定使用fopen来打开文件,用fclose来关闭文件。
fopen
FILE* fopen(const char* filename, const char* mode);
文件成功打开时,返回的是指向特定文件信息区的结构体指针;打开失败返回的是空指针,所以,在使用文件的时候,得要进行判断。
一些重要的文件的打开方式
| 文件的打开方式 | 具体意义 | 文件不存在 |
|---|---|---|
| “r” | 为了输入数据打开文件 | 报错 |
| “w” | 为了输出数据打开文件 | 创建一个新文件 |
| “rb” | 为了输入数据,打开一个二进制文件 | 报错 |
| “wb” | 为了输出数据,打开一个二进制文件 | 创建一个新文件 |
fclose
int fclose(FILE* stream);
举例
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main()
{
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return;
}
//文件相关操作
//...
fclose(pf);
return 0;
}
文件的顺序读写
| 功能 | 函数名 | 使用范围 |
|---|---|---|
| 字符输入 | fgetc | 所有输入流 |
| 字符输出 | fputc | 所有输入流 |
| 文本输入 | fgets | 所有输入流 |
| 文本输出 | fputs | 所有输入流 |
| 格式化输入 | fscanf | 所有输入流 |
| 格式化输出 | fprintf | 所有输入流 |
| 二进制输入 | fread | 文件 |
| 二进制输出 | fwrite | 文件 |
文件的随机读写
fseek
int fseek(FILE* stream, long offset, int origin);
该函数有3个参数,第一个是一个文件指针,第二个是偏移量,第三个是起始位置。
起始位置要是第一个元素则为0,最后一个元素则为-1。
通过这3个参数可以定位到文件的相关位置,进行读写操作。
ftell
long ftell(FILE* stream);
要是该函数能正常返回的话,返回的是文件指针相较于起始位置的偏移量。
rewind
void rewind(FILE* stream);
让文件指针指向文件的起始位置。
文件读取结束的判断
feof
int feof(FILE* stream);
这个函数是用于文件结束,判断是读取失败还是遇到文件结束。
- 判断文本文件是否读取结束:
fgetc判断是否为EOF
fgets判断是否为NULL
- 判断二进制文件是否读取结束
判断返回值是否小于实际要读的个数,fread判断返回值是否小于实际要读的个数。
边栏推荐
- 图的相关操作
- Sequential storage structure, chain storage structure and implementation of stack
- Is there any inconspicuous but profitable sideline?
- C语言 libcurl交叉编译
- Food safety | eight questions and eight answers take you to know crayfish again! This is the right way to eat!
- 绘制pdf表格 (一) 通过itext实现在pdf中绘制excel表格样式并且实现下载(支持中文字体)
- 泛域名配置方法
- What are the advantages of real-time cloud rendering
- 排序还需要了解的信息以及链表
- 云流化和云桌面有什么关系
猜你喜欢

Food safety | eight questions and eight answers take you to know crayfish again! This is the right way to eat!

云VR:虚拟现实专业化的下一步

Introduction to cloud XR and development opportunities of cloud XR in 5g Era

"Digital security" alert NFT's seven Scams

BiSeNet v1

Redis source code and design analysis -- 15. RDB persistence mechanism

Interviewer: talk about log The difference between fatal and panic

为什么数字化未来取决于3D实时渲染

Cloud XR面临的问题以及Cloud XR主要应用场景

PageHelper can also be combined with lambda expressions to achieve concise paging encapsulation
随机推荐
「行话」| 用DevOps高效交付游戏,是种什么体验?
大话DevOps监控,团队如何选择监控工具?
Problems faced by cloud XR and main application scenarios of cloud XR
泛域名配置方法
How to choose digital twin visualization platform
云VR:虚拟现实专业化的下一步
CH582 BLE 5.0 使用 LE Coded 广播和连接
Creation of unity Bezier curve
imx6 RTL8189FTV移植
BL602 开发环境搭建
Lwip之内存与包缓冲管理
Sequential storage structure, chain storage structure and implementation of stack
BiSeNet v1
MySQL optimistic lock
Principle and implementation of UDP penetration NAT in P2P
PageHelper can also be combined with lambda expressions to achieve concise paging encapsulation
Basic knowledge of software testing (mind mapping)
Good news! Ruiyun technology was awarded the member unit of 5g integrated application special committee of "sailing on the sea"
Postman快速上手
RedisTemplate解决高并发下秒杀系统库存超卖方案 — Redis事务+乐观锁机制