C语言 为什么我在第二次搜索文件时出现分段错误


#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>

#define BUFSIZ 6

在这里,我在文件中搜索 3 个字符的代码,如果代码与令牌匹配,它应该输出您输入的代码。

void country_search(char code[6], FILE *dir, FILE *BAC_ptr){
    char *token_off;
    char *token;
    char buf[512];
    while (!feof(dir)){
        if (fgets(buf, 512 , dir)){
            token = strtok(buf,"1234567890");
            while (token != NULL){
                token = strtok(NULL,"1234567890");
                code = strtok(code,"n");
                if ((strcmp(code,token)) == 0){
                printf("token: %sn", token);
                }
            }
        }   
    }
}
int main(int argc, const char **argv) {
    char code[6];
    FILE *BAC_ptr;
    BAC_ptr = fopen("BinaryAllCountries.dat", "r");
    FILE *D_ptr;
    D_ptr = fopen("directory.dat", "r");
    fprintf(stdout, "Hello! welcome to the country database press type in: quit[enter] to quit at anytimen");
    fprintf(stdout, "enter character code of the country you want:n ");
    fgets(code, BUFSIZ, stdin);

当我使用 while 循环时,它会要求第一个输入然后接受它,运行 country_search() 方法然后返回,第二次它给了我一个分段错误错误,我不知道为什么。

        while (strcmp(code, "quitn") != 0) {
        fprintf(stdout, "enter another character code of the country you want: n");
            country_search(code, D_ptr, BAC_ptr);
            fgets(code, BUFSIZ, stdin);
        }   
}

这是我的目录.dat文件

ABW3739AFG1511AGO43AIA3690ALB2635AND2654ANT4227ARE2547ARG5281ARM1532ASM4607ATA1336ATF1392ATG3721AUS4631AUT2675AZE1556BDI170BEL2717BEN62BFA149BGD1600BGR2775BHR1574BHS3759BIH2753BLR2695BLZ3797BMU3816BOL5302BRA5324BRB3778BRN1639BTN1620BVT1357BWA84CAF252CAN3837CCK4691CHE3597CHL5344CHN1684CIV355CMR193COD327COG307COK4715COL5367COM288CPV216CRI3887CUB3906CXR4658CYM3863CYP1703CZE2825DEU2956DJI376DMA3926DNK2846DOM3958DZA22ECU5389EGY396ERI447ESH1275ESP3519EST2867ETH470FIN2913FJI4740FLK5416FRA2934FRO2892FSM4857GAB489GBR3646GEO1745GHA529GIB2975GIN549GLP4044GMB509GNB574GNQ426GRC2996GRD4023GRL4004GTM4068GUF5442GUM4786GUY5459HKG1768HMD1433HND4109HRV2796HTI4087HUN3058IDN1813IND1790IOT122IRL3099IRN1832IRQ1851ISL3078ISR1871ITA3117JAM4130JOR1912JPN1892KAZ1936KEN592KGZ1980KHM1662KIR4806KNA4328KOR2356KWT1956LAO1998LBN2019LBR632LBY668LCA4352LIE3162LKA2380LSO613LTU3185LUX3208LVA3137MAC2037MAR818MCO3288MDA3270MDG691MDV2081MEX4174MHL4834MKD3231MLI731MLT3249MMR2125MNG2103MNP5001MOZ843MRT755MSR4194MTQ4153MUS776MWI712MYS2060MYT796NAM864NCL4900NER884NFK4965NGA907NIC4250NIU4940NLD3314NOR3334NPL2145NRU4874NZL4925OMN2189PAK2213PAN4270PCN5065PER5498PHL2262PLW5018PNG5048POL3355PRI4295PRK2171PRT3377PRY5481PSE2236PYF4769QAT2280REU927ROM3399RUS3433RWA947SAU2307SDN1142SEN1025SGP2330SGS1485SHN970SJM3552SLB5111SLE1073SLV3983SMR3455SOM1095SPM4388STP1004SUR5519SVK3477SVN3499SWE3572SWZ1165SYC1047SYR2400TCA4500TCD270TGO1206THA2468TJK2445TKL5129TKM2513TMP1724TON5146TTO4464TUN1227TUR2487TUV5164TWN2421TZA1188UGA1248UKR3617UMI5208URY5540USA4529UZB2572VAT3036VCT4433VEN5564VGB4555VIR4581VNM2594VUT5228WLF5257WSM5083YEM2614YUG3671ZAF1122ZMB1295

程序在以下行崩溃(valgrind立即告诉我):

if ((strcmp(code,token)) == 0){

为什么?因为您测试得太早NULL

    while (token != NULL){
        token = strtok(NULL,"1234567890"); <- right here TOKEN can be set to NULL!
        /* code = strtok(code,"n"); MUST be moved elsewhere*/
         if ((strcmp(code,token)) == 0){
           printf("token: %sn", token);
         }

要解决,只需在适当的位置插入NULL检查:

while (token != NULL) {
    token = strtok(NULL,"1234567890");
    if (token)
    {
        if (strcmp(code,token) == 0){ /* no need for extra parens here */
          printf("token: %sn", token);
        }
    }
}

补充几点:

  1. 不使用BAC_PTR
  2. ncode中移除的线移动到安全的地方。最好在环外...但更好的是,
  3. 输入空行也会使此崩溃。因此,在输入后直接删除n,然后测试空行。
  4. 您的 512 缓冲区不好。对于整个directory.dat文件来说,它太小了,其中一个 3 个字母的代码被切成两半。

相关内容

  • 没有找到相关文章

最新更新