C语言文件详解(二)文件的顺序读写

一、文件顺序读写函数及其关系示意图

文件的顺序读写函数的函数功能和适用于如下图:

 内存与文件之间的函数操作以及数据的输入输出示意图如下图:

 

二、函数详细介绍与示范

1.fputc(字符输出函数)(写"w")

作用: 将字符character写入到文件指针stream所指的文件中去。

注意!!!返回的为int 型,返回的为字符的ASCII值

读取成功,返回字符的ASCII值,读取失败返回EOF。

示范代码:

#include<stdio.h>
int main()
{
	//相对路径
	//打开文件,新建test.txt文件(注意!!所创建的文件在当前工程test_10_13文件路径下)
	FILE*pf = fopen("test.txt","w");

	if (pf==NULL)
	{
		perror("fopen");
		return 1;
	}
	//写文件
	//fputc('a',pf);   将字符a写入到文件指针pf所指的文件里去。
	int i = 0;
	for (i = 0; i < 26;,i++)
	{
		fputc('a'+i,pf);
	}
			
	//关闭文件
	fclose(pf);
	pf = NULL;
	return 0;
}

 运行结果:


2.fgetc(字符输入函数)(读"r")

作用: 读取文件指针stream所指的文件中的字符。

注意!!!返回的为int 型,返回的为字符的ASCII值

读取成功,返回字符的ASCII值,读取失败返回EOF。

 示范代码:

#include<stdio.h>
int main()
{
	//相对路径
	//打开文件,新建test.txt文件(注意!!所创建的文件在当前工程test_10_13文件路径下)
	FILE*pf = fopen("test.txt","r");

	if (pf==NULL)
	{
		perror("fopen");
		return 1;
	}
	//读文件
	int ch = fgetc(pf);
	printf("%c\n",ch);

	ch = fgetc(pf);
	printf("%c\n", ch);

	ch = fgetc(pf);
	printf("%c\n", ch);
	//关闭文件
	fclose(pf);
	pf = NULL;
	return 0;
}

文件内容:

 打印结果:

 注意!!!一个读字符后,文件中的指针自动会指向下一个字符。

优化代码:读取成功返回ASCII值,失败返回EOF。

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

3.fputs(文本行输出函数)(写"w")

作用:将字符串str写入文件中去,写入的字符串无换行(需换行可自行加\n)

示范代码;

	FILE*pf = fopen("test.txt","w");

	if (pf==NULL)
	{
		perror("fopen");
		return 1;
	}
	//写文件,一行一行的写
	fputs("hello",pf);
	fputs("word", pf);

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

运行结果:

 4.fgets(文本行输入函数)(读"r")

作用:最多读取文件中的num个字符到str中去。

其中,str为读取字符所存放的位置,num为最多读取的字符数。

注意!!!真实读取的字符为num-1个!!!

代码示范:

	FILE*pf = fopen("test.txt","w");

	if (pf==NULL)
	{
		perror("fopen");
		return 1;
	}
	//读文件,一行一行的读

	char arr[] = "##########";
	fgets(arr,5,pf);

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

文件内字符:

 

调试结果:

代码中num =5,最大读取5个字符,实际值读取了'h','e','l''l'四个字符,所以读取的最后一个字符为‘\n’

实际读取了num-1个字符即5-1=4个字符。

所以打印的时候无需加'\n'换行,因为所读取的最后一个字符即为'\n'!!!!!

	char arr[20] = "##########";
	fgets(arr,20,pf);
	printf("%s",arr);
	fgets(arr, 20, pf);
	printf("%s", arr);

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

 5.fprintf(格式化输出函数)(写"w")

                                  

 作用:将数据按照相应的格式写入输出到文件中去。

fprintf对比于printf,fprintf函数多了一个参数FILE * stream。

示范代码:

#include<stdio.h>
struct s
{
	char name[20];
	int age;
	float score;
};
int main()
{
	struct s stu = { "zhangsan", 20, 90.5f }; //初始化结构体
	FILE*pf = fopen("text.txt","w");  //新建文件
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//写文件
	fprintf(pf,"%s %d %f",stu.name,stu.age,stu.score);

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

文件内容:

  5.fscanf(格式化输入函数)(读"r")

                                       

  作用:格式化读取文件中的数据。

fscanf对比于scanf,fscanf函数多了一个参数FILE * stream。

示范代码:

#include<stdio.h>
struct s
{
	char name[20];
	int age;
	float score;
};
int main()
{
	struct s stu = { 0 };
	FILE*pf = fopen("text.txt","r");
	if (pf==NULL)
	{
		perror("fopen");
		return 0;
	}
	//读文件
	fscanf(pf,"%s %d %f",stu.name,&(stu.age),&(stu.score));
	printf("%s %d %f", stu.name, stu.age, stu.score);
	//关闭文件
	fclose(pf);
	pf = NULL;
	return 0;
}

运行结果:

  6.fwrite(二进制输入)(写"w")

   作用:从指针ptr开始所指向的数据里面,一次写上count个大小为size的数据到文件里面去。

示范代码:

#include<stdio.h>
struct s
{
	char name[20];
	int age;
	float score;
};
int main()
{
	struct s stu = {"zahngsan",20,90.5f}; //初始化结构体
	FILE*pf = fopen("text.txt","wb");  //新建文件,"wb"二进制写
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//写文件
	fwrite(&stu,sizeof(stu),1,pf);//将结构体stu写入文件中。

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

文件内容:二进制xign

 7.fread(二进制输出)(读"r")

  作用:从文件读取count个大小为size的数据到ptr所指向的空间里去。

示范代码:

#include<stdio.h>
struct s
{
	char name[20];
	int age;
	float score;
};
int main()
{
	struct s stu = { 0 }; //初始化结构体
	FILE*pf = fopen("text.txt", "rb");  //新建文件,"rb"二进制读
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//写文件
	fread(&stu, sizeof(stu), 1, pf);//将文件中的数据读入到stu里去。
	printf("%s %d %f",stu.name,stu.age,stu.score);
	//关闭文件
	fclose(pf);
	pf = NULL;
	return 0;
}

运行结果:

 三、额外补充

fgetc,fputc,fgets,fputs,fscanf,fprintf适用于所有输出流/输入流,而printf,scanf只适用于标准输出流/标准输入流。

那么如何通过fgetc,fputc来实现标准标准输出流/标准输入流的输出,输入(从键盘上读,屏幕上写)呢?

 

对于任何一个C程序,只要运行起来,就默认打开3个流:

stdin --标准输入流--键盘

stdout--标准输出流--屏幕   

stderr--标准错误流--屏幕

注意!!!!stdin,stdout,stderr都是FILE*类型。

示范代码:

#include<stdio.h>
int main()
{
	int ch = fgetc(stdin);  //从标准输入流输入
	fputc(ch,stdout);  //从标准输出流输出。
	return 0;
}

运行结果:

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在 C 语言中,二进制文件普通文本文件的过程有一些不同,需要使用二进制模式打开文件,并使用适当的函数进行操作。以下是一个简单的示例代码,用于在 C 语言中进行二进制文件操作: ```c #include <stdio.h> #include <stdlib.h> struct student { char name[50]; int age; float weight; }; int main() { FILE *fp; struct student stu; // 打开二进制文件 fp = fopen("students.dat", "wb+"); // 入结构体到文件 strcpy(stu.name, "John"); stu.age = 18; stu.weight = 65.5; fwrite(&stu, sizeof(struct student), 1, fp); // 定位到文件开始 rewind(fp); // 文件内容 fread(&stu, sizeof(struct student), 1, fp); printf("文件内容:\n"); printf("姓名:%s\n", stu.name); printf("年龄:%d\n", stu.age); printf("体重:%f\n", stu.weight); // 关闭文件 fclose(fp); return 0; } ``` 这个例子中,程序首先使用 `fopen()` 函数打开一个名为 `students.dat` 的二进制文件,并使用 `wb+` 模式打开,可以进行入和取操作。然后,程序使用 `fwrite()` 函数将一个 `struct student` 结构体文件中。接着,程序使用 `rewind()` 函数将文件指针定位到文件开始,然后使用 `fread()` 函数文件内容到 `struct student` 结构体变量中。最后,程序使用 `fclose()` 函数关闭文件。需要注意的是,在二进制文件时,需要使用 `fwrite()` 和 `fread()` 函数,并且在调用这两个函数时,需要指定要的数据类型的大小和数量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值