#include <stdio.h>
#include <string.h>
#include <stdlib.h> struct stu
{
char no[8];
char name[10];
int age;
float chinese;
float math;
float english;
} record; main()
{
char yn = 'n', source[8];
int go = 1;
FILE *fps;
printf("输入文件名(如a.txt) :");
scanf("%8s", source);
if ((fps = fopen(source, "a+")) == NULL)
{
printf("不能打开%s文件!\n", source);
exit(1);
}
do
{
printf("学号:");
fflush(stdin);
scanf("%8s", record.no);
printf("姓名:");
fflush(stdin);
scanf("%10s", record.name);
printf("年龄:");
fflush(stdin);
scanf("%d", &record.age);
printf("语文:");
fflush(stdin);
scanf("%.1f", &record.chinese);
printf("数学:");
fflush(stdin);
scanf("%.1f", &record.math);
printf("英语:");
fflush(stdin);
/*下面这句,写不进去就要报错*/
scanf("%.1f", &record.english); fprintf(fps, "%d\t%s\t%s\t%d\t%.2f\t%.2f\t%.2f\n", go++, *record.no, *record.name, record.age, record.chinese, record.math, record.english);
/*--------------------------*/
printf("是否继续增加(Y/N):");
fflush(stdin);
scanf("%c", &yn);
} while (yn == 'y' || yn == 'Y');
fclose(fps);
}
谢谢了。
#include <string.h>
#include <stdlib.h> struct stu
{
char no[8];
char name[10];
int age;
float chinese;
float math;
float english;
} record; main()
{
char yn = 'n', source[8];
int go = 1;
FILE *fps;
printf("输入文件名(如a.txt) :");
scanf("%8s", source);
if ((fps = fopen(source, "a+")) == NULL)
{
printf("不能打开%s文件!\n", source);
exit(1);
}
do
{
printf("学号:");
fflush(stdin);
scanf("%8s", record.no);
printf("姓名:");
fflush(stdin);
scanf("%10s", record.name);
printf("年龄:");
fflush(stdin);
scanf("%d", &record.age);
printf("语文:");
fflush(stdin);
scanf("%.1f", &record.chinese);
printf("数学:");
fflush(stdin);
scanf("%.1f", &record.math);
printf("英语:");
fflush(stdin);
/*下面这句,写不进去就要报错*/
scanf("%.1f", &record.english); fprintf(fps, "%d\t%s\t%s\t%d\t%.2f\t%.2f\t%.2f\n", go++, *record.no, *record.name, record.age, record.chinese, record.math, record.english);
/*--------------------------*/
printf("是否继续增加(Y/N):");
fflush(stdin);
scanf("%c", &yn);
} while (yn == 'y' || yn == 'Y');
fclose(fps);
}
谢谢了。