C语言学习——fscanf()返回值

已排序的学生成绩文件进行插人处理。插人一个学生的3门课程成绩,程序先计算新插人学生的平均成绩,然后将它按成绩高低顺序插入,插入后不建立一个新文件。

#include<stdio.h>
#include<stdlib.h>

struct Student {
	int number;
	char name[10];
	int score[3];
	float ave;
}stu[3], stue;
int main() {
	FILE* fp, * fp2;
	char string[100], c;
	int n = 0, t = 0;


	if ((fp = fopen("1.txt", "rb")) == NULL) {
		printf("未找到该文件");
		exit(0);
	}
	scanf("%d%s%d%d%d", &stue.number, stue.name, &stue.score[0], &stue.score[1], &stue.score[2]);
	stue.ave = (float)(stue.score[0] + stue.score[1] + stue.score[2]) / 3;

	for (int i = 0; (fscanf(fp, "%d%s%d%d%d%f", &stu[i].number, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2], &stu[i].ave))!=0; i++) {		
		n++;
		printf("n:%d\n");
		
	}
	printf("n:%d\n", n);
	for (int j = 0; j < 3; j++) {
		if (stue.ave < stu[j].ave) {
			t++;
		}
	}

	if ((fp2 = fopen("1.txt", "w")) == NULL) {
		printf("未找到该文件");
		exit(0);
	}

	for (int i = 0; i < t; i++) {
		fprintf(fp2, "%d %s %d %d %d %f\n", stu[i].number, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].ave);
		printf(" % d % s % d % d % d % f\n", stu[i].number, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].ave);
	}
	fprintf(fp2, "%d %s %d %d %d %f\n", stue.number, stue.name, stue.score[0], stue.score[1], stue.score[2], stue.ave);
	printf(" % d % s % d % d % d % f\n", stue.number, stue.name, stue.score[0], stue.score[1], stue.score[2], stue.ave);

	for (int i = t; i < n; i++) {
		fprintf(fp2, "%d %s %d %d %d %f\n", stu[i].number, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].ave);
		printf(" % d % s % d % d % d % f\n", stu[i].number, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].ave);
	}
}

结果:死循环

原因:fscanf()并未读取到数据时的返回值不是0。

fread()返回值为实际读取到的文件内容的块数,到结尾返回0。

fscanf()则返回的是实际读取的数据个数,出错或者到结尾返回EOF。

修改后:

#include<stdio.h>
#include<stdlib.h>

struct Student {
	int number;
	char name[10];
	int score[3];
	float ave;
}stu[3], stue;
int main() {
	FILE* fp, * fp2;
	char string[100], c;
	int n = 0, t = 0;


	if ((fp = fopen("1.txt", "rb")) == NULL) {
		printf("未找到该文件");
		exit(0);
	}
	scanf("%d%s%d%d%d", &stue.number, stue.name, &stue.score[0], &stue.score[1], &stue.score[2]);
	stue.ave = (float)(stue.score[0] + stue.score[1] + stue.score[2]) / 3;

	for (int i = 0; (fscanf(fp, "%d%s%d%d%d%f", &stu[i].number, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2], &stu[i].ave)) !=EOF; i++) {
		n++;		

	}
	printf("n:%d\n", n);
	for (int j = 0; j < 3; j++) {
		if (stue.ave < stu[j].ave) {
			t++;
		}
	}

	if ((fp2 = fopen("1.txt", "w")) == NULL) {
		printf("未找到该文件");
		exit(0);
	}

	for (int i = 0; i < t; i++) {
		fprintf(fp2, "%d %s %d %d %d %f\n", stu[i].number, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].ave);
		printf(" % d %s % d % d % d % f\n", stu[i].number, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].ave);
	}
	fprintf(fp2, "%d %s %d %d %d %f\n", stue.number, stue.name, stue.score[0], stue.score[1], stue.score[2], stue.ave);
	printf(" % d %s % d % d % d % f\n", stue.number, stue.name, stue.score[0], stue.score[1], stue.score[2], stue.ave);

	for (int i = t; i < n; i++) {
		fprintf(fp2, "%d %s %d %d %d %f\n", stu[i].number, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].ave);
		printf(" %d %s %d %d % d % f\n", stu[i].number, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].ave);
	}
}

结果:

看书上的答案是fread(&stu[i],sizeof(struct Student),1,fp)!=0,就认为fscanf()并未读取到数据时的返回值是0,还是基础不够扎实。

 

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
`fprintf` 和 `fscanf` 是 C 语言中用于文件输入输出的函数。 `fprintf` 函数用于将数据格式化输出到文件中。它的语法如下: ```c int fprintf(FILE *stream, const char *format, ...); ``` 其中,`stream` 是一个指向要写入的文件的指针,`format` 是一个格式化字符串,后面可以跟上要输出的数据。`fprintf` 函数返回成功写入的字符数,如果发生错误则返回负数。 举个例子,下面的代码将会把一个整数和一个字符串输出到文件中: ```c #include <stdio.h> int main() { FILE *fp = fopen("output.txt", "w"); int number = 42; char str[] = "hello world"; fprintf(fp, "The number is %d and the string is %s", number, str); fclose(fp); return 0; } ``` `fscanf` 函数则是用于从文件中读取格式化数据的函数。它的语法如下: ```c int fscanf(FILE *stream, const char *format, ...); ``` 其中,`stream` 是一个指向要读取的文件的指针,`format` 是一个格式化字符串,后面可以跟上要读取的数据的地址。`fscanf` 函数返回成功读取的数据项数,如果发生错误则返回负数。 举个例子,下面的代码会从文件中读取一个整数和一个字符串: ```c #include <stdio.h> int main() { FILE *fp = fopen("input.txt", "r"); int number; char str[100]; fscanf(fp, "%d %s", &number, str); fclose(fp); printf("The number is %d and the string is %s\n", number, str); return 0; } ``` 注意,`fscanf` 函数在读取字符串时需要指定缓冲区的大小,以避免缓冲区溢出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老板来根肠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值