当前位置:网站首页>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;
}
原网站

版权声明
本文为[BSP Junior Primary School Monk]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208092342344217.html