#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
//char paixu[50];//名次
char id[150]; //学号
char nam[20];//姓名
int score; //成绩
}stu[10000];
struct student temp;
int main()
{
int a, b = 0;
int score90_100 = 0, score80_89 = 0, score70_79 = 0, score0_69 = 0;
int j = 0, i = 0, q=0, m=0, k=0, l=0;
FILE* p, * fp;
char in[20], out[20];
printf("请输入输入文件地址");
scanf("%s", in);
printf("请输入输出文件地址");
scanf("%s", out);
if ((p = fopen(in, "r")) == NULL)
{
printf("can't\n");
exit(0);
}
if ((fp = fopen(out, "w")) == NULL)
{
printf("can't\n");
exit(0);
}
while (!feof(p))
{
fscanf(p, " %s %s %d\n", &stu[i].id, &stu[i].nam, &stu[i].score);
i++;
b++;
}
for (i = 0; i < b - 1; i++)
{
for (j = i + 1; j < b; j++)
{
if (stu[i].score < stu[j].score)
{
temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
for (i = 0; i < b; i++)
{
fprintf(fp, "%d\t %s\t %s\t %d\t", i + 1, stu[i].id, stu[i].nam, stu[i].score);
fprintf(fp, "%c", '\n');
if (stu[i].score > 90)
q++;
if (stu[i].score > 80)
m++;
if (stu[i].score > 70)
k++;
if (stu[i].score < 69)
l++;
}
for (m = 0; m < b; m++)
if (stu[m].score >= 90) score90_100++;
else if (stu[m].score >= 80) score80_89++;
else if (stu[m].score >= 70) score70_79++;
else if (stu[m].score <= 69) score0_69++;
printf("优秀(90~100)%d人数,百分比;%.1f%%\n ", score90_100, 100.0 * score90_100 / b);
printf("良好(80~89)%d人数,百分比;%.1f%%\n ", score80_89, 100.0 * score80_89 / b);
printf("中等(70~79)%d人数,百分比;%.1f%%\n ", score70_79, 100.0 * score70_79 / b);
printf("较差(0~69)%d人数,百分比;%.1f%%\n ", score0_69, 100.0 * score0_69 / b);
fprintf(fp, "优秀(90~100)%d人数,百分比;%.1f%%\n ", score90_100, 100.0 * score90_100 / b);
fprintf(fp, "良好(80~89)%d人数,百分比;%.1f%%\n ", score80_89, 100.0 * score80_89 / b);
fprintf(fp, "中等(70~79)%d人数,百分比;%.1f%%\n ", score70_79, 100.0 * score70_79 / b);
fprintf(fp, "较差(0~69)%d人数,百分比;%.1f%%\n ", score0_69, 100.0 * score0_69 / b);
fclose(fp);
printf("\n结果已经写入%s\n", fp);
return 0;
}
因为你用了exit(0);啊,这个是终止程序的命令。
看你程序,应该是你文件地址有误,导致读取和写入的时候,文件对象都为空,就执行了exit(0)方法。
exit(0)可是暴力终止啊
再说你都can't了,比如文件打不开,继续下去其实也没有什么意义啊
printf("can't\n");
exit(0);
打开文件失败,不是你叫程序退出执行的吗?
打开文件的时候出错了,要么是你读写的文件路径不对,要么就是文件在C盘(系统盘),程序没权限读取。如有帮助,请采纳一下,谢谢。
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632