当前位置:网站首页>(C语言)文件的基本操作
(C语言)文件的基本操作
2022-07-26 20:32:00 【付青云同学】
大家好呀!这个是付青云同学的博客!
目前一直在学习C语言。
写博客是为了来记录我的学习过程,同时也希望通过博客能够帮助到需要帮助的人。
如果我的博客可以帮助到你,不妨给我一个关注哦
文件操作
文件名包含:文件路径+文件名主干+文件名后缀
如:C:\Program Files (x86)\test.txt
文件的打开和关闭
文件指针
缓冲文件系统中,关键的概念是“文件类型指针”,简称“文件指针”。
每个被使用的文件都在内存中开辟了一个相应的文件信息区,用来存放文件的相关信息(如文件的名字,文件状态及文件当前的位置等)。
这些信息是保存在一个结构体变量中的。该结构体类型是有系统声明的,取名FILE
打开和关闭文件
| 文件使用方式 | 含义 | 如果指定文件不存在 |
|---|---|---|
| “r”(只读) | 为了输入数据,打开一个已经存在的文本文件 | 出错 |
| “w”(只写) | 为了输出数据,打开一个文本文件 | 建立一个新文件 |
| “a”(追加) | 向文本文件添加数据 | 建立一个新文件 |
| “rb”(只读) | 为了输入数据,打开一个二进制文件 | 出错 |
| “wb”(追加) | 为了输出数据,打开一个二进制文件 | 建立一个新文件 |
| “ab”(读写) | 像一个二进制文件尾添加数据 | 出错 |
| “r+”(读写) | 为了读和写,打开一个文本文件 | 出错 |
| “w+”(读写) | 为了读和写,建立一个新的文件 | 建立一个新文件 |
| “a+”(读写) | 为了打开一个文件,在文件尾进行读写 | 建立一个新文件 |
| “rb+”(读写) | 为了读和写打开一个二进制文件 | 出错 |
| “wb+”(读写) | 为了读和写,新建一个新的二进制文件 | 建立一个新文件 |
| “ab+”(读写) | 打开一个二进制文件,在文件尾进行读和写 | 建立一个新文件 |
如:
int main()
{
FILE* pf = fopen("abc.txt", "r");//只读打开文件
if (pf == NULL)
{
perror("fopen");
return 1;
}
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
代码的顺序读写
| 功能 | 函数名 | 适用于 |
|---|---|---|
| 字符输入函数 | fgetc | 所有输入流 |
| 字符输出函数 | fputc | 所有输出流 |
| 文本行输入函数 | fgets | 所有输入流 |
| 文本行输出函数 | fputs | 所有输出流 |
| 格式化输入函数 | fscanf | 所有输入流 |
| 格式化输出函数 | fprintf | 所有输出流 |
| 二进制输入 | fread | 文件 |
| 二进制输出 | fwrite | 文件 |
文件的随机读写
fseelk
作用:根据文件指针的位置和偏移量来定位文件指针(将文件指针移到指定位置)
//声明
int fseek ( FILE * stream, long int offset, int origin );
//offset origin 中的字节数。
//origin 初始位置。
#include <stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "wb");//打开一个二进制文件
if (pf == NULL)
{
perror("fopen");
exit(-1);
}
fputs("wo 1234 kenobi", pf);//写入文件
fseek(pf, 3, SEEK_SET);//将文件指针移动到第三位
fputs("jiao", pf);
fclose(pf);//关闭文件
pf = NULL;
return 0;
}
运行后test.txt里显示为
wo jiao kenobi
ftell
作用:返回文件指针相对于起始位置的偏移量(获取文件指针的当前位置)
//声明
long int ftell ( FILE * stream );
用法:
#include <stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "wb");//打开一个二进制文件
if (pf == NULL)
{
perror("fopen");
exit(-1);
}
fputs("wo 1234 kenobi", pf);
int size = ftell(pf);
printf("%d\n", size);
fseek(pf, 3, SEEK_SET);
size = ftell(pf);
printf("%d\n", size);
fclose(pf);
pf = NULL;
return 0;
}
运行 如图:
rewind
作用:让文件指针的位置回到文件的起始位置
//声明
void rewind ( FILE * stream );
用法:
int main()
{
FILE* pf = fopen("test.txt", "wb");//打开一个二进制文件
if (pf == NULL)
{
perror("fopen");
exit(-1);
}
fputs("wo 1234 kenobi", pf);
int size = ftell(pf);
printf("%d\n", size);
rewind(pf);
size = ftell(pf);
printf("%d\n", size);
fclose(pf);
pf = NULL;
return 0;
}
运行 如图:
边栏推荐
- 2022 open atom global open source summit agenda express | list of sub forum agenda on July 27
- Summary of common interview questions of computer composition principle, including answers
- [HCIA security] bidirectional nat
- 【HCIA安全】双向NAT
- 加载 iframe 时显示加载指示器
- 除了「加机器」,其实你的微服务还能这样优化
- JDBC connection
- 牛客刷题——Mysql系列
- How to block the legendary GEE engine version? Close player account tutorial through script + engine
- Set the template of core configuration file in idea
猜你喜欢

如何在一个项目中使用多种不同的语言?

idea中设置核心配置文件的模板
![[MySQL series] - how much do you know about the index](/img/d7/5045a846580be106e2bf16d7b30581.png)
[MySQL series] - how much do you know about the index

牛客刷题——Mysql系列

Alkbh1

Devops has been practiced for many years. What is the most painful thing?

2022开放原子全球开源峰会议程速递 | 7 月 27 日分论坛议程一览

SPI configuration
HTTP cache browser cache that rabbits can understand

Some unexpected bug records
随机推荐
Devsecops, speed and security
How to implement Devops with automation tools | including low code and Devops application practice
[must read new] Keya valuation analysis of University of technology, heating energy-saving products
Summary of common interview questions of computer composition principle, including answers
【HCIA安全】双向NAT
【HCIA安全】NAT网络地址转换
【HCIE安全】双机热备-主备备份
<button> 与 <input type=“button“ />
自定义注解(一)
Soft test --- database (1) database foundation
牛客多校-Journey-(建图distra+卡常优化)
Browser browser cache
基于Hough变换的直线检测(Matlab)
Devops has been practiced for many years. What is the most painful thing?
记一次invalid bound statement xxxxxx 问题解决思路
Line detection based on Hough transform (matlab)
Modify excel default code
(C语言)浅识#define
Some unexpected bug records
In addition to "adding machines", in fact, your micro service can be optimized like this