#include <stdio.h>
int main()
{
FILE *fp = fopen("read.txt", "w");
int studentCount;
printf("How many students are there?");
scanf("%i", &studentCount);
//fprintf()用来向文件写入内容, 分两步, 一先存放变量中, 二利用这个变量读入
fprintf(fp, "%i\n", studentCount); //在文件中先输入一个数再换行
for(int i = 0; i < studentCount; ++i)
{
char studentname[30] = {0};
float gpa;
printf("What is the student's last name?\n");
scanf("%s", &studentname); //要先存放变量, 后面才用fprintf写入
printf("What is the student's GPA?\n");
scanf("%i", &gpa); //先存放变量, 后面利这这个变量用
//fprintf()读入文件内
//下面是利用fprintf()向文件读入变量内容
fprintf(fp, "%i\t%s\t%.2f\n", i, studentname, gpa);
}
printf("读入成功!\n");
fclose(fp);
return 0;
}