c语言题目,在线等

2024-12-04 03:38:16
推荐回答(2个)
回答1:

#include
struct student
{
char name[10];
int number;
int age;
char add[20];

}; struct student stu[4];
void main()
{
FILE *fp;
int i;
for(i=0;i<3;i++)
{
printf("输入第 %d个学生的姓名 :",i+1);
scanf("%d",stu[i].name);
printf("输入第 %d 个学生的学号:",i+1);
scanf("%d",&stu[i].number);
printf("输入第 %d 个学生的年龄:",i+1);
scanf("%s",&stu[i].age);
printf("输入第 %d 个学生的地址:",i+1);
scanf("%s",stu[i].add);
}
for(i=0;i<4;i++)
{
fprintf(fp,"namem\number\age\add\n");
fprintf(fp,"%s\t%d\t\t%d\t%s\n",stu[i].name,stu[i].number,stu[i].age,stu[i].add);
}
fclose(fp);
}
希望能帮到你

回答2:

#include
#include

struct student{
char name[5];
char address[10];
int age;
int stu_num;
};

void inputstu(student stu[],int n)
{
for(int i=0;i {
printf("input a student's infomation:\n");
printf("name: ");
scanf("%s",&stu[i].name);
printf("number : ");
scanf("%d",&stu[i].stu_num);
printf("address: ");
scanf("%s",&stu[i].address);
printf("age: ");
scanf("%d",&stu[i].age);
}
printf("input complete\n");
}
void outtofile(student stu[],int n)
{
FILE * fout;
fout=fopen("stu_info.txt","w");
for(int i=0;i {
fprintf(fout,"%s",stu[i].name);
fprintf(fout,"%s",stu[i].address);
fprintf(fout,"%d",stu[i].age);
fprintf(fout,"%d\n",stu[i].stu_num);
}
printf("save complete!\n");
fclose(fout);
}
void main()
{
student stu[3];
inputstu(stu,3);
outtofile(stu,3);
}