为什么第二次搜索文件时出现分段错误错误?

时间:2017-01-30 21:29:38

标签: c file segmentation-fault

#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: %s\n", 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 anytime\n");
    fprintf(stdout, "enter character code of the country you want:\n ");
    fgets(code, BUFSIZ, stdin);

当我使用while循环时,它要求输入第一个输入,然后通过country_search()方法然后返回,第二次它给我一个分段错误错误,我无法弄清楚这是为什么。

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

这是我的directory.dat文件

ABW3739AFG1511AGO43AIA3690ALB2635AND2654ANT4227ARE2547ARG5281ARM1532ASM4607ATA1336ATF1392ATG3721AUS4631AUT2675AZE1556BDI170BEL2717BEN62BFA149BGD1600BGR2775BHR1574BHS3759BIH2753BLR2695BLZ3797BMU3816BOL5302BRA5324BRB3778BRN1639BTN1620BVT1357BWA84CAF252CAN3837CCK4691CHE3597CHL5344CHN1684CIV355CMR193COD327COG307COK4715COL5367COM288CPV216CRI3887CUB3906CXR4658CYM3863CYP1703CZE2825DEU2956DJI376DMA3926DNK2846DOM3958DZA22ECU5389EGY396ERI447ESH1275ESP3519EST2867ETH470FIN2913FJI4740FLK5416FRA2934FRO2892FSM4857GAB489GBR3646GEO1745GHA529GIB2975GIN549GLP4044GMB509GNB574GNQ426GRC2996GRD4023GRL4004GTM4068GUF5442GUM4786GUY5459HKG1768HMD1433HND4109HRV2796HTI4087HUN3058IDN1813IND1790IOT122IRL3099IRN1832IRQ1851ISL3078ISR1871ITA3117JAM4130JOR1912JPN1892KAZ1936KEN592KGZ1980KHM1662KIR4806KNA4328KOR2356KWT1956LAO1998LBN2019LBR632LBY668LCA4352LIE3162LKA2380LSO613LTU3185LUX3208LVA3137MAC2037MAR818MCO3288MDA3270MDG691MDV2081MEX4174MHL4834MKD3231MLI731MLT3249MMR2125MNG2103MNP5001MOZ843MRT755MSR4194MTQ4153MUS776MWI712MYS2060MYT796NAM864NCL4900NER884NFK4965NGA907NIC4250NIU4940NLD3314NOR3334NPL2145NRU4874NZL4925OMN2189PAK2213PAN4270PCN5065PER5498PHL2262PLW5018PNG5048POL3355PRI4295PRK2171PRT3377PRY5481PSE2236PYF4769QAT2280REU927ROM3399RUS3433RWA947SAU2307SDN1142SEN1025SGP2330SGS1485SHN970SJM3552SLB5111SLE1073SLV3983SMR3455SOM1095SPM4388STP1004SUR5519SVK3477SVN3499SWE3572SWZ1165SYC1047SYR2400TCA4500TCD270TGO1206THA2468TJK2445TKL5129TKM2513TMP1724TON5146TTO4464TUN1227TUR2487TUV5164TWN2421TZA1188UGA1248UKR3617UMI5208URY5540USA4529UZB2572VAT3036VCT4433VEN5564VGB4555VIR4581VNM2594VUT5228WLF5257WSM5083YEM2614YUG3671ZAF1122ZMB1295

1 个答案:

答案 0 :(得分:0)

程序在以下行崩溃(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: %s\n", token);
         }

要解决此问题,只需在适当的位置插入NULL支票:

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

一些额外的评论:

  1. BAC_PTR未使用。
  2. 移动将\ncode移到某处安全的行。最好是在循环之外...但更好,
  3. 输入空行也会导致此崩溃。因此,请在输入后直接删除\n,然后测试空行。
  4. 512的缓冲区不好。它对于整个directory.dat文件来说太小了,其中一个3个字母的代码被减少了一半。