我编写了一个应该写入文件的代码,但是当我执行该程序时,它会显示“保存学生数据时出错”。这就是代码。
#include<stdio.h>
typedef struct Student{
int numberOfStudents;
char name; // onoma foithth
char surname; // epi8eto foithth
};
int main(){
struct Student s1;
FILE *file=fopen("d:\\student.txt","w");
if(file==NULL){
printf("error in saving student data");
return 1;
}
while(1){
printf("Enter number of students: ");
scanf("%d",&s1.numberOfStudents);
printf("enter name: ");
scanf("%s",&s1.name);
printf("enter surname: ");
scanf("%s",&s1.surname);
fprintf(file,"%d\t%s\t%s\n",s1.numberOfStudents,s1.name,s1.surname);
printf("continue (Y/N)");
char ch=getch();
if (ch=='N' || ch=='n')
break;
}
fclose(file);
return 0;
}
我已经搜索过但我找不到问题。我的错误在哪里?
答案 0 :(得分:0)
char array
结构字段应为onoma foithth
,如果您想将typedef struct Student{
int numberOfStudents;
char name[10]; // onoma foithth
char surname; // epi8eto foithth
};
存储在其中,如图所示。将结构修改为
name
然后在扫描&
时删除name
,因为scanf("%s",s1.name);
本身就是地址。
fopen()
确保您在sed -r ':a;/^\s*abc:/!b;:b;$d;N;/^(\s*)abc.*\n\1[^\n]*$/{/^(\s*)abc.*\n\1\S/!bb};s/.*\n//;ba' file