用C语言做 输入几个同学的姓名、学号、三门功课成绩,写入到磁盘文件中(文件名自定)

发布时间:2024-11-01 07:21 发布:上海旅游网

问题描述:

输入几个同学的姓名、学号、三门功课成绩,写入到磁盘文件中(文件名自定)用C 语言做

问题解答:

#include <stdio.h>
main()
{
FILE *fp;
char ch,*fn;
stuct student{
char *nm;int id,a,b,c;
}std;
printf("Please input a file name:\n");
gets(fn);
if(fp=fopen(fn,"a+")){
do {
printf("\nPlease input a student's name,id,score a,score b,score c\n");
scanf("%s %d %d %d %d %d",std.nm,&std.id,&std.a,&std.b,&std.c);
fwrite(std,sizeof(struct student),1,fp);
printf("\nNext:[y/n]");
scanf("%c",ch);
}while(ch!='n'&&ch!='N');

}
else printf("The file cannot be openned!");

fclose(fp);
}

热点新闻