C语言文件 *** 作

C语言文件 *** 作,第1张

文件 程序文件

包括源程序文件(后缀为.c),目标文件(windows环境后缀为.obj),可执行程序(windows环境后缀为.exe)。

数据文件

文件的内容不一定是程序,而是程序运行时读写的数据,比如程序运行需要从中读取数据的文件,
或者输出内容的文件。

文件类型

数据文件称为文本文件或者二进制文件

数据在内存中以二进制形式存储,不转换的输出到外存,就是二进制文件

如果以ASCII码的形式输出到外存中,以ASCII字符形式存储的文件是文本文件

字符用ASCII形式存储,数值型数据可以用ASCII形式存储,也可以用二进制形式存储

文件缓冲区

从内存向磁盘输出数据会先送到内存中的缓冲区,装满缓冲区后才一起送到磁盘上。缓冲区的大小根据C编译系统决定的。

文件的打开关闭 文件指针 FILE

每个被使用的文件都在内存中开辟了一个相应的文件信息区,用来存放文件的相关信息(如文件的名字,文件状态及文件当前的位置等)。这些信息是保存在一个结构体变量中的。该结构体类型是有系统声明的,取名FILE。

文件 *** 作的一些函数 fputs(在文件中写入一个字符)
#include
#include
#include
int main()
{
	FILE* pf=fopen("test.txt","w");
	if(pf==NULL)
	{
		printf("%s\n",strerror(errno));
	}
	fputc('n',pf);
	fputc('i',pf);
	fputc('h',pf);
	fputc('a',pf);
	fputc('o',pf);
	fputc('n',pf);
	fputc('i',pf);
	fclose(pf);
	pf=NULL;
	return 0;
}

在文件 test.txt 上显示 nihao

fgets(在文件中读出一个字符)
#include
#include
#include
int main()
{
	FILE* pf=fopen("test.txt","r");
	if(pf==NULL)
	{
		printf("%s\n",strerror(errno));
	}
	printf("%c",fgetc(pf));
	printf("%c",fgetc(pf)); 
	printf("%c",fgetc(pf));
	printf("%c",fgetc(pf));
	printf("%c",fgetc(pf));
	fclose(pf);
	pf=NULL;
	return 0;
}

编译结果:nihao

 

标准输入输出流
#include
int main()
{
	int ch=fgetc(stdin);
	fputc( ch,stdout);
	return 0;
} 

输入什么字符就显示什么字符

例如输入 n

编译结果:n

 

fputs(在文件中写入一行字符串)
#include
#include
#include
int main()
{
	FILE* pf = fopen("text.txt","w");
	if(pf==NULL)
	{
		printf("%s\n",strerror(errno));
	}
	fputs("nihaoa\n",pf);
	fputs("are you ok?\n",pf);
	fclose(pf);
	pf=NULL;
	return 0;
}

在text,txt文件中显示:

nihaoa

are you ok?

fgets(从文件中读取一行字符串) 
#include
#include
#include
int main()
{
	char arr[1024]={0};
	FILE* pf = fopen("text.txt","r");
	if(pf==NULL)
	{
		printf("%s\n",strerror(errno));
	}
	fgets(arr,1024,pf);
	printf("%s",arr);
	fclose(pf);
	pf=NULL;
	return 0;
}

提前在 text.txt 文件中写入 Hello!

编译结果:Hello!

#include
#include
#include
int main()
{
	char arr[1024]={0};
	FILE* pf = fopen("text.txt","r");
	if(pf==NULL)
	{
		printf("%s\n",strerror(errno));
	}
	fgets(arr,1024,pf);
	printf("%s",arr);
	fgets(arr,1024,pf);
	printf("%s",arr);
	fclose(pf);
	pf=NULL;
	return 0;
}

提前在 text.txt 文件中写入

Hello!

World!

编译结果:

Hello!

World!

解释:因为用了两行fgets函数,每用一次,读取一行的字符串。

标准输入输出写法 
#include
int main()
{
	char arr[1024]={0};
	fgets(arr,1024,stdin);
	fputs(arr,stdout);
	return 0;
}

输入:Hello World!

输出:Hello World!

fprintf函数 (将格式化的数据写入文件中)
#include
#include
#include
struct stu 
{
	int n;
	char name[20];
	float socre;
};
int main()
{
	struct stu s={20,"zhangsan",90.5};
	FILE* pf=fopen("text.txt","w");
	if(NULL==pf)
	{
		printf("%s\n",strerror(errno));
	}
	fprintf(pf,"%d  %s  %lf",s.n,s.name,s.socre);
	return 0;
	fclose(pf);
	pf=NULL;
}

text.txt 文件中显示:20  zhangsan  90.500000

 

fscnaf函数(将文件中的数据写入的格式化中)
#include
#include
#include
struct stu 
{
	int n;
	char name[20];
	float socre;
};
int main()
{
	struct stu s={0};
	FILE* pf=fopen("text.txt","r");
	if(NULL==pf)
	{
		printf("%s\n",strerror(errno));
	}
	fscanf(pf,"%d  %s  %f",&(s.n),s.name,&(s.socre));
	printf("%d  %s  %f\n",s.n,s.name,s.socre);
	fclose(pf);
	pf=NULL;
	return 0;
}

编译结果:20  zhangsan  90.500000

标准化的输入和输出
#include
#include
#include
struct stu 
{
	int n;
	char name[20];
	float socre;
};
int main()
{
	struct stu s={0};
	fscanf(stdin,"%d  %s  %f",&(s.n),s.name,&(s.socre));//输入数据写入到结构体中
	fprintf(stdout,"%d  %s  %f",s.n,s.name,s.socre);//将格式化的数据输出出来
	return 0;
}

输入:21  lisi  89.5

输出:21  lisi  89.500000

sprintf和sscnaf(将格式化数据转成字符串形式(sprintf)或者字符串数据转成格式化(sscanf))
#include
struct stu
{
	int n;
	float age;
	char name[20];
};
int main()
{
	struct stu s={20,91.5,"xiebiesan"};
	struct stu temp={0};
	char arr[1024]={0};
	sprintf(arr,"%d %f %s",s.n,s.age,s.name);//将格式化的数据(结构体)转换成字符串的数据,存储在arr中。
	printf("%s\n",arr);
	sscanf(arr,"%d %f %s",&(temp.n),&(temp.age),temp.name);//从arr中读取格式化的数据,存储在结构体temp中。
	printf("%d %f %s",temp.n,temp.age,temp.name);
} 

编译结果:

20 91.500000 xiaobiesan
20 91.500000 xiaobiesan

一些函数的对比 scanf/printf

针对标准输入流/输出流的格式化输入输出语句

fscanf/fprintf

针对所有输入流/输出流的格式化输入输出语句

sscanf/sprintf

 sscanf:是从字符串中读取格式化的数据

sprintf:是把格式化数据输出成(存储到)字符串

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/3002317.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-27
下一篇 2022-09-27

发表评论

登录后才能评论

评论列表(0条)

保存