|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
void showcode(void){
FILE *fp;
char name1[10], name2[10], name3[10];
char code1[4], code2[4], code3[4];
char buffer[20];
if((fp=fopen("CityBranchCodes_Leyang_Hu_20215416.txt", "r"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
printf("\n");
printf("City\t\t\tcode\t\t\tCity\t\t\tcode\t\t\tCity\t\t\tcode\n");
printf("_____________________________________________________________________________________________________________________________\n");
while(fgets(buffer, 20, fp)!=NULL){
int len=strlen(buffer);
buffer[len-1]='\0';
char *code=strtok(buffer, "\t");
char *name=strtok(NULL, "\t");
if(strlen(code)==3){
int rmd=(atoi(code))%3;
switch(rmd){
case 1:
strcpy(name1, name);
strcpy(code1, code);
break;
case 2:
strcpy(name2, name);
strcpy(code2, code);
break;
case 0: strcpy(name3, name);
strcpy(code3, code);
printf("%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\n", name1, code1, name2, code2, name3, code3);
break;
}
}
}
printf("%-10s\t\t%-10s\n", name1, code1);
printf("\n");
fseek(fp, 0, SEEK_SET);
printf("Enter the city code: ");
char *citycode;
scanf("%s", citycode);
getchar();
printf("\n");
if((fp=fopen("CityBranchCodes_Leyang_Hu_20215416.txt", "r"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
while(fgets(buffer, 20, fp)!=NULL){
if(buffer==citycode){
break;
}
}
while(fgets(buffer, 20, fp)!=NULL){
len=strlen(buffer);
buffer[len-1]='\0';
code=strtok(buffer, "\t");
name=strtok(NULL, "\t");
if(strlen(code)==3){
break;
}
rmd=(atoi(code))%3;
switch(rmd){
case 1:
strcpy(name1, name);
strcpy(code1, code);
break;
case 2:
strcpy(name2, name);
strcpy(code2, code);
break;
case 0: strcpy(name3, name);
strcpy(code3, code);
printf("%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\n", name1, code1, name2, code2, name3, code3);
break;
}
}
switch(rmd){
case 1:
printf("%-10s\t\t%-10s\n", name1, code1);
break;
case 2:
printf("%-10s\t\t%-10s\t\t%-10s\t\t%-10s\n", name1, code1, name2, code2);
break;
case 0:
break;
}
fclose(fp);
}
报没定义错误是因为你没有在函数开头定义,而是在使用这些变量的时候才临时定义,在别处使用时,如果脱离作用域,就会很容易报出这种错误,这个习惯很不好。你可以试试把这些变量定义提前到函数开头。
|
|