我正在尝试编写一个程序,该程序将在学生数据库中搜索姓氏并在该姓氏所在位置打印整行。每次我运行它时,它都说没有匹配。
void search(){
FILE *file;
file = fopen("students.txt", "r");
char sSurrname[100];
char id[10];
char name[10];
char the_name[100];
char surrname[10];
char phone[10];
char the_surrname[100];
char phone_number[100];
int the_id;
printf("Type searched surrname: ");
scanf("%s", &sSurrname);
while (!feof(file)){
fscanf(file, "%s %d || %s %s || %s %s || %s %s ||", &id, &the_id, &name, &the_name, &surrname, &the_surrname, &phone, &phone_number);
if (strcmp(sSurrname, the_surrname) == 0) {
printf("%s %d || %s %s || %s %s || %s %s ||", &id, &the_id, &name, &the_name, &surrname, &the_surrname, &phone, &phone_number);
break;
} else {
printf("No Match.");
break;
continue;
}
这是数据库:
ID:0 ||姓名:Pepa ||姓:Hnatek ||电话:321 |||
ID:1 ||姓名:Jan ||姓氏:Novak ||电话:123 |||
将phone_number
声明为整数,像fscanf
和fscanf(file, "%s %d || %s %s || %s %s || %s %s ||", id, &the_id, name, the_name, surrname, the_surrname, phone, &phone_number);
一样调整printf...
。拆下第二个break
。这样就可以了。