当前位置:网站首页>Basic operation of (C language) files
Basic operation of (C language) files
2022-07-26 21:50:00 【Fu Qingyun】
Hello, everyone ! This is Fu Qingyun's blog !
I've been learning C Language .
I write a blog to record my learning process , At the same time, I hope to help people in need through blog .
If my blog can help you , Might as well give me a concern
File operations
The filename contains : File path + File name trunk + File name suffix
Such as :C:\Program Files (x86)\test.txt
Opening and closing of files
The file pointer
Buffer file system , The key concept is “ File type pointer ”, abbreviation “ The file pointer ”.
Each used file has a corresponding file information area in memory , It is used to store information about files ( Such as the name of the document , File status and current file location, etc ).
This information is stored in a structure variable . The structure type is system declared , The name FILE
Open and close files
| How files are used | meaning | If the specified file does not exist |
|---|---|---|
| “r”( read-only ) | To enter data , Open an existing text file | error |
| “w”( Just write ) | To output data , Open a text file | Create a new document |
| “a”( Additional ) | Add data to a text file | Create a new document |
| “rb”( read-only ) | To enter data , Open a binary file | error |
| “wb”( Additional ) | To output data , Open a binary file | Create a new document |
| “ab”( Reading and writing ) | Add data at the end of a binary file | error |
| “r+”( Reading and writing ) | For reading and writing , Open a text file | error |
| “w+”( Reading and writing ) | For reading and writing , Create a new file | Create a new document |
| “a+”( Reading and writing ) | To open a file , Read and write at the end of the file | Create a new document |
| “rb+”( Reading and writing ) | Open a binary file for reading and writing | error |
| “wb+”( Reading and writing ) | For reading and writing , Create a new binary file | Create a new document |
| “ab+”( Reading and writing ) | Open a binary file , Read and write at the end of the file | Create a new document |
Such as :
int main()
{
FILE* pf = fopen("abc.txt", "r");// Read only open file
if (pf == NULL)
{
perror("fopen");
return 1;
}
// Close file
fclose(pf);
pf = NULL;
return 0;
}
Sequential reading and writing of code
| function | Function name | Apply to |
|---|---|---|
| Character input function | fgetc | All input streams |
| Character output function | fputc | All output streams |
| Text line input function | fgets | All input streams |
| Text line output function | fputs | All output streams |
| Format input function | fscanf | All input streams |
| Format output function | fprintf | All output streams |
| Binary input | fread | file |
| Binary output | fwrite | file |
Random reading and writing of documents
fseelk
effect : Locate the file pointer according to its position and offset ( Move the file pointer to the specified location )
// Statement
int fseek ( FILE * stream, long int offset, int origin );
//offset origin The number of bytes in .
//origin initial position .
#include <stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "wb");// Open a binary file
if (pf == NULL)
{
perror("fopen");
exit(-1);
}
fputs("wo 1234 kenobi", pf);// write file
fseek(pf, 3, SEEK_SET);// Move the file pointer to the third position
fputs("jiao", pf);
fclose(pf);// Close file
pf = NULL;
return 0;
}
After operation test.txt It is shown as
wo jiao kenobi
ftell
effect : Returns the offset of the file pointer from its starting position ( Get the current location of the file pointer )
// Statement
long int ftell ( FILE * stream );
usage :
#include <stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "wb");// Open a binary file
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;
}
function Pictured :
rewind
effect : Return the file pointer to the beginning of the file
// Statement
void rewind ( FILE * stream );
usage :
int main()
{
FILE* pf = fopen("test.txt", "wb");// Open a binary file
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;
}
function Pictured :
边栏推荐
- Number() VS parseInt()
- [MySql]substr用法-查询表的某个字段的具体位数的值
- 方法重载与方法重写
- 五、小程序报错:message:Error: 系统错误,错误码:80058,desc of scope.userLocation is empty
- Supplement - nonlinear programming
- 分布式 session 的4个解决方案
- Flask对token的解码&挂载&装饰器&七牛云上传
- Resume in 2022 is dead in the sea. Don't vote. Software testing positions are saturated
- 【音视频】ijkplayer播放器参数说明文档
- Content management tools, blue bookmarks are enough
猜你喜欢

6、 Wechat applet release process

FreeRTOS personal notes - Events

Supplement - nonlinear programming

Jd.com: how does redis realize inventory deduction? How to prevent goods from being oversold?

Solution to the problem of sticking and unpacking TCP

Also on Data Governance

Summer vacation daily question week 7:7.18 - 7.24

月薪5万的朋友告诉我,你只是在打杂

成功上岸了自动化测试岗,最高月薪15.4K,自己真棒~

逻辑漏洞----任意账号注册
随机推荐
基于CAShapeLayer和贝塞尔曲线的圆形进度条动画
cmake编译obs-studio-27.2.0
contenteditable 元素的placeholder
Selenium automated test interview questions family bucket
基于memcache的缓存机制的6个指令
[MySql]substr用法-查询表的某个字段的具体位数的值
Solution to the problem of sticking and unpacking TCP
Resume in 2022 is dead in the sea. Don't vote. Software testing positions are saturated
《暑假每日一题》Week 7:7.18 - 7.24
Selenium自动化测试面试题全家桶
Can I view the history in the "stealth" mode of the secure browser?
If you do not add waitkey() function after imshow() function, it will not be displayed
工业基础类IFC—提取模型结构树
美国再次发难:禁止承包商采购这5家中国公司的设备与技术
Pytoch squeeze() unsqueeze() usage
会用redis吗?那还不快来了解下redis protocol
FreeRTOS个人笔记-事件
The United States once again challenged: Forbidding contractors to purchase the equipment and technology of these five Chinese companies
JS click the picture to print the image
虾皮shopee根据关键词取商品列表 API