C语言文件操作:打开关闭,读写

程序文件

源程序文件(后缀为.c)
目标文件(Windows环境后缀为.obj)
可执行文件(Windows环境后缀为.exe)

fputc

	FILE* pf = fopen("test.txt","w");
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 1;
	}

	//写文件
	char i = 0;
	for (i = 'a'; i <= 'z'; i++)
	{
		fputc(i, pf);
	}

fgetc

	FILE* pf = fopen("test.txt","r");
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 1;
	}

	//读文件
	int ch = 0;
	while ((ch=fgetc(pf)) != EOF)
	{
		printf("%c ",ch);

	}

fputs

	FILE* pf = fopen("test.txt","w");  //w在文件打开的时候就清理掉数据了
	 //写文件的时候,如果有内容,会销毁再重新写数据
	//FILE* pf = fopen("test.txt", "a");  写a可以实现追加

	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 1;
	}

	//写一行数据
	fputs("hello world\n", pf);

fgets

	FILE* pf = fopen("test.txt","r");  
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}

	//读一行数据
	char arr[20];
	fgets(arr, 5, pf); //读5个字符,但自己加一个\0,实际读四个
	printf("%s\n", arr);  //hello中只读了hell

fprintf
printf是打印输出到屏幕,fprintf是打印输出到文件。


struct s
{
	char arr[10];
	int age;
	float score;

};
int main()
{	
	struct s s = { "zs",22,23.3f };

	FILE* pf = fopen("test.txt","w");  
	if (pf == NULL)
	{
		perror("fopen"); //fopen是自己加的
		return 1;
	}

	
	fprintf(pf,"%s %d %f", s.arr, s.age, s.score);

	//关闭文件
	fclose(pf);
	pf = NULL;

	return 0;
}

fscanf
格式化输入函数

struct s s = { 0 };

	FILE* pf = fopen("test.txt","r");  
	if (pf == NULL)
	{
		perror("fopen"); //fopen是自己加的
		return 1;
	}


	fscanf(pf, "%s %d %f", s.arr, &(s.age), &(s.score));
	printf("%s %d %f", s.arr, s.age, s.score);

任何一个c程序,只要运行起来,就会默认打开三个流
它们的类型都是FILE*
FILE* stdin - 标准输入流 (键盘)
FIEL* stdout - 标准输出流(屏幕)
FILE* stderr - 标准错误流(也是屏幕)

	fprintf(stdout,"%s %d %f", s.arr, s.age, s.score);  //打印到屏幕上去

fwrite
以二进制的形式写进文件中

	struct s s = {"zhangsan",25,50.5f};

	FILE* pf = fopen("test.txt","wb");  
	if (pf == NULL)
	{
		perror("fopen"); 
		return 1;
	}


	fwrite(&s, sizeof(struct s), 1, pf);

fread
以二进制的方式读

struct s s = {0};

	FILE* pf = fopen("test.txt","rb");  
	if (pf == NULL)
	{
		perror("fopen"); 
		return 1;
	}


	fread(&s, sizeof(struct s), 1, pf);
	printf("%s %d %f", s.arr, s.age, s.score);

scanf是针对标准输入的格式化输入语句
printf是针对标准输入的格式化输出语句
fscanf是针对所有输入流的格式化输入语句
fprintf是针对所有输入流的格式化输出语句
sscanf从一个字符串中转化成一个格式化的数据 (序列化)
sprintf 把一个格式化的数据转化成字符串(反序列化)

	struct s s = {"zhangsan",20,55.5f};
	struct s tmp = { 0 };
	char buf[100] = { 0 };
	sprintf(buf, "%s %d %f", s.arr, s.age, s.score);
	//sprintf 把一个格式化的数据写到字符串中,本质是把一个格式化的数据转换成字符串
	printf("%s\n", buf);
	sscanf(buf, "%s %d %f", tmp.arr, &(tmp.age), &(tmp.score)); //从字符串中获取一个格式化的数据到tmp中
	printf("%s %d %f\n", tmp.arr, tmp.age, tmp.score);

在这里插入图片描述
fseek定位文件指针,ftell计算当前文件指针的偏移量

	fseek(pf, 2 ,SEEK_SET); //文件内容abcdef  直接定位到c

	int ch = fgetc(pf); 
	printf("%c ", ch);
	printf("%d\n", ftell(pf)); //3

	fseek(pf, 2, SEEK_CUR); //文件内容abcdef  直接定位到f

	ch = fgetc(pf);
	printf("%c ", ch);
	printf("%d\n", ftell(pf)); //6

	fseek(pf, -1, SEEK_END);
	ch = fgetc(pf);
	printf("%c ", ch);
	printf("%d\n", ftell(pf)); //6

rewind让文件指针位置回到文件起始位置

	rewind(pf);
	ch = fgetc(pf);
	printf("%c ", ch); //a

feof应用于当文件读取结束的时候,判断是读取失败结束还是文件尾结束

文本文件读取是否结束判断,判断返回值是否为EOF或者NULL
fgetc判断是否为EOF
fgets判断返回值是否为NULL

二进制文件的读取结束判断判断返回值是否小于实际要读的个数
fread判断返回值是否小于实际要读的个数

	//判断是什么原因结束的
	if (ferror(pf))
	{
		puts("I?O error when reading ");
	}
	else if (feof(pf))
	{
		puts("end of the file reached successfully");
	}

文件缓冲区
因为有缓冲区的存在,C语言在操作文件的时候,需要做刷新缓冲区或者在文件操作结束的时候关闭文件
如果不做,可能导致读写文件的问题

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值