在数据库中搜索

问题描述 投票:1回答:1

我正在尝试编写一个程序,该程序将在学生数据库中搜索姓氏并在该姓氏所在位置打印整行。每次我运行它时,它都说没有匹配。

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 |||

c scanf
1个回答
0
投票

phone_number声明为整数,像fscanffscanf(file, "%s %d || %s %s || %s %s || %s %s ||", id, &the_id, name, the_name, surrname, the_surrname, phone, &phone_number);一样调整printf...。拆下第二个break。这样就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.