当前位置:网站首页>Summary of basic operations of c language files
Summary of basic operations of c language files
2022-08-10 01:48:00 【BSP Junior Primary School Monk】
博客主页:https://blog.csdn.net/weixin_46094737?type=blog
欢迎评论留言 如有错误敬请指正!
本文由小学生廉原创,首发于 CSDN
未来很长,值得我们全力奔赴更美好的生活!
一、文件
We are almost inseparable from files in the use of computers.例如常见的有word 文档,txt文本文件,图片文件、音频文件等.
1.什么是文件?
文件是以计算机硬盘为载体存储在计算机上的信息集合.It is a type of data source,最主要的作用是保存数据.
在程序设计中,We can divide files into two categories:程序文件和数据文件
(1)程序文件
包括源程序文件(后缀为.c),目标文件(windows环境后缀为.obj),可执行程序文件(windows环境后缀为.exe).
(2)数据文件
文件的内容不一定是程序,而是程序运行时读写的数据,比如程序运行需要从中读取数据的文件,或者输出内容的文件.
2.文件名
文件名是文件存在的标识,操作系统根据文件名来对其进行控制和管理.Each file is given a specified name,由文件主名和扩展名组成.
即:一个文件要有一个唯一的文件标识,以便用户识别和引用.
文件名包含三个部分:文件路径+文件名主干+文件后缀
例:D:\Windows Kits\10\Lib\test.txt
二、文件的打开和关闭
1.文件指针
We perform a series of operations on the file——打开文件、关闭文件、向文件中写入数据、从文件中读出数据.All operations are inseparable from the file pointer.
文件指针: FILE*
FILE:每个被使用的文件都在内存中开辟了一个相应的文件信息区,用来存放文件的相关信息(文件的名字,文件状态及文件当前的位置等).这些信息是保存在一个结构体变量中,取名FILE.
FILE* :通过一个FILE的指针来维护这个FILE结构的变量,对文件进行操作.
(1)fopen函数
在C语言中,The file should be opened before manipulating it.使用 <stdio.h> 头文件中的 fopen() 函数可以打开文件.
FILE *pf=NULL;
pf=fopen("test.txt","r"); //只读
(2)fclose函数
在执行完文件的操作后,要用 fclose() 函数进行“关闭文件”操作.This frees up related resources,避免数据丢失.At the same time, the overall execution efficiency of the system can be improved.
fclose() 的用法为:fclose(FILE *fp);
fclose(pf);
File opening methods are divided into the following categories:
文件打开方式 | 含义 | 若指定文件不存在 |
“r”(只读) | 打开一个存在的文本文件,然后读取数据 | 出错 |
“w”(只写) | 打开一个存在的文本文件,然后写入数据(注意会清空) | 创建一个新文件 |
“a”(追加) | Append data at the end of the text file | 出错 |
“r+”(读写) | 打开一个文本文件,可读可写 | 出错 |
“w+”(读写) | 打开一个文本文件,可读可写 | 创建一个新文件 |
“a+”(读写) | 打开一个文本文件,可读可写,Append data at the end of the text | 创建一个新文件 |
三、文件的顺序读写
After opening the file, you need to perform specific operations on the file,The following are the functions for reading and writing files
功能 | 函数名 |
---|---|
字符输入函数 | fgetc |
字符输出函数 | fputc |
文本行输入函数 | gets |
文本行输出函数 | fputs |
格式化输入函数 | fscanf |
格式化输出函数 | fprintf |
二进制输入 | fread |
二进制输出 | fwrite |
Pay attention to two when usingfread函数以及fwrite函数的用法
fread(target read,类型字节数,Open up reading space,file to be read)
fwrite(目标写入,类型字节数,Open up write space,写入文件)
fread函数
fread(str02,1,5,pf);
fwrite函数
FILE *pf;
char str[]="123456";
char str02[20];
pf=fopen("fire_fread.txt","w");
fwrite(str,1,7,pf);
fclose(pf);
练习源代码:
#include <stdio.h>
void fire_test(void)
{
FILE *pf=NULL;
pf=fopen("test.txt","r"); //只读
pf=fclose;
pf=fopen("test.txt","w");//只写
fputc('a',pf);
pf=fopen("test.txt","a");//追加
fputc('1',pf);//Appends a character to the file's contents'1';
pf=fopen("test.txt","r");
printf("指针pf的地址为:%#p\n",pf);
char ch;
ch=fgetc(pf);
printf("%c",ch);
fclose(pf);
pf=fopen("test02.txt","r");
printf("指针pf的地址为:%#p\n",pf);
if(NULL==pf)
{
printf("无此文件!");
}
else
{
fputc(50,pf);
}
}
void fire_fputs(void)
{
char str[20]="换羽重生!";
FILE *pf;
pf=fopen("fire_fputs.txt","w");
fputs(str,pf);
fputc('\n',pf);
fputs("首战必胜!",pf);
fclose(pf);
}
void fire_a(void)
{
char str[20]="test r-w-a";
FILE *pf;
pf=fopen("fire_fputs.txt","a");
fputs(str,pf);
fclose(pf);
}
void fire_fgets(void)
{
char str[40];
printf("str=%s\n",str);
FILE *pf;
pf=fopen("fire_fgets.txt","r");
if(NULL==pf)
{
printf("无此文件");
}
else
{
fgets(str,40,pf);
}
printf("str=%s",str);
fclose(pf);
}
void fire_fread(void)
{
FILE *pf;
char str[]="123456";
char str02[20];
pf=fopen("fire_fread.txt","w");
fwrite(str,1,7,pf);
fclose(pf);
pf=fopen("fire_fread.txt","r");
fread(str02,1,5,pf);
printf("%s",str02);
fclose(pf);
}
int main()
{
fire_fread();
// fire_test();
// fire_fputs();
// fire_fgets();
// fire_a();
return 0;
}
边栏推荐
- 由生物素参与的D-Biotinol,CAS号:53906-36-8具体特性说明
- Involved in PEG-Biotin (CAS: 1778736-18-7) Biotin-PEG4-OH is widely used in molecular target detection
- Are the numbers entered symmetrical?
- Kubernetes 60个为什么
- [C language] Address book "Static Memory Version"
- Creo5.0入门教程赠素材
- FITC标记生物素(FITC-生物素|CAS:134759-22-1)有哪些知识了?
- Today's sleep quality record 61 points
- 最高月薪15K,谁有历经千辛万苦的意志,谁就能收获属于自己的成功~
- Mysql database ALTER basic operations
猜你喜欢
随机推荐
When knowledge and action are one
将string类对象中的内容格式化到字符串Buffer中时遇到的异常崩溃分析
天猫全网商品详情封装接口
Koa中间件next实现
Mysql数据库 ALTER 基本操作
CAS:851113-28-5 (Biotin-ahx-ahx-tyramine)
基于FPGA的任意字节数的串口接收(含源码工程)
Are the numbers entered symmetrical?
Minimum number of steps to get out of the maze 2
阿雷的血压有些低
C language learning journey [operator (incomplete version)]
Redisson 分布式锁
Project (7) - PolarSeg point cloud semantic segmentation
Characteristics of the (CAS:1527486-16-3TAMRA-azide-PEG3-Biotin) reaction in biotin azide!
Win7怎么把控制面板添加到右键菜单
Leetcode82. 删除排序链表中的重复元素 II
03|流程控制
为什么不建议你在 Docker 中跑 Mysql ?
dlopen failed: library "libtaml.so" not found
Next.js获取路由参数及styled-jsx 的使用