#include<stdio.h>
typedef struct student{
char name[8];
long num;
int score;
}student ;
#define N 5
void main()
{
int i,j;
struct student stu[N];
FILE *fp;
char ch;char str1[5]="STU\n";char str2[5]="0";char str3[5]="\n";
{
fp = fopen("student.stu","wb+");
fputs(str1,fp);
fclose(fp);
}//写入STU标志
{
fp = fopen("student.stu","ab+");
printf("请输入您的班级名称\n");
scanf("%s",str2);
fputs(str2,fp);
}//写入用户班级名
fputs(str3,fp);
fclose(fp);//换行
printf("该文件前缀班级已经写入\n");
{
for(i=0;i<N;i++)
{
printf("请输入第%d个学生的档案:\n",i+1);
printf("学号:");scanf("%d",&stu[i].num);
printf("成绩:");scanf("%d",&stu[i].score);
printf("姓名:");scanf("%s",stu[i].name);
}//定义结构体并输入数据
for(i=0;i<N;i++)
{
fp = fopen("student.stu","ab+");fwrite(stu[i].name,sizeof(student),1,fp);fputs(str3,fp);fclose(fp);
fp = fopen("student.stu","ab+");fwrite(&stu[i].num,sizeof(student),1,fp);fputs(str3,fp);fclose(fp);
fp = fopen("student.stu","ab+");fwrite(&stu[i].score,sizeof(student),1,fp);fputs(str3,fp);fclose(fp);
}
printf("该文件已经写入学生信息\n");
}//输入与写入
fp = fopen("student.stu","ab+");
fread(str1,sizeof(str1),1,fp);
fclose(fp);
fp = fopen("student.stu","ab+");
fread(str2,sizeof(str2),1,fp);
fclose(fp);
for(i=0;i<N;i++)
{
fp = fopen("student.stu","ab+");fread(stu[i].name,sizeof(student),1,fp);fclose(fp);printf("\n");
fp = fopen("student.stu","ab+");fread(&stu[i].num,sizeof(student),1,fp);fclose(fp);printf("\n");
fp = fopen("student.stu","ab+");fread(&stu[i].score,sizeof(student),1,fp);fclose(fp);printf("\n");
}
printf("已经显示完毕\n");
}
c语言文件,向student.stu写入五个结构体关于学生信息数据,并显示数据在屏幕上
最新推荐文章于 2023-11-18 19:34:05 发布