有没有大佬帮我看一下为什么编译说我空白行以下的程序code,name,len, rmd没定义
void showcode(void){FILE *fp;
char name1, name2, name3;
char code1, code2, code3;
char buffer;
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='\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='\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);
} 就是从发fseek后面开始。我单独运行前半部分是可行的,所以我后半部分用的同样的思路来打印,但是编译就说我code,name,len,rmd没定义。可是这些在我的前半部分程序都已经定义过了,人晕了{:10_291:} 建议把报错内容复制过来 报没定义错误是因为你没有在函数开头定义,而是在使用这些变量的时候才临时定义,在别处使用时,如果脱离作用域,就会很容易报出这种错误,这个习惯很不好。你可以试试把这些变量定义提前到函数开头。
页:
[1]