分段故障没有明显原因发生

时间:2012-12-01 22:46:39

标签: c segmentation-fault

我没有明显的理由得到分段错误。我正在使用strtok函数并将每一行拆分为多个标记并将它们存储在char指针中。我的数据如下:

输入:

200 -> 103 [weight=7];
200 -> 153 [weight=27];
200 -> 53 [weight=9];
200 -> 178 [weight=43];
55 -> 2 [weight=23];
55 -> 14 [weight=50];
55 -> 20 [weight=17];
55 -> 22 [weight=1];
55 -> 74 [weight=7];
55 -> 93 [weight=9];
55 -> 122 [weight=27];
65 -> 8 [weight=27];
65 -> 9 [weight=9];

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc,char **argv)
{
        char *ipfile,line[80];
        FILE *fp;
        char *field1,*field2,*field3,*field4,*field5,*field6,mystr[10];
        int row,column,weight;
        ipfile = (char *)argv[1];
        printf("The input file name is %s\n",ipfile);
        fp = fopen(ipfile,"r");

        if(fp ==NULL) //Checking whether the command line argument was correctly or not.
        printf("There is no such file in the directory.\n");

        while(fgets(line,80,fp) != NULL)
        {
                field1 = strtok(line," ");
                //row    = atoi(field1);
                field2 = strtok(NULL," ");
                field3 = strtok(NULL," ");
                //column = atoi(field3);
                field4 = strtok(NULL," ");
                field5 = strtok(NULL," ");
                //field6 = strtok(NULL," ");

                printf("%s\n",field5);
                //printf("Row-%d Column - %d Weight - %d\n",row,column,weight);
        }
        fclose(fp);
        return 0;
}

来自评论:

  

当我尝试打印field1field2field3field4时,会打印出来。但是,当我尝试field5field6时,我的程序会出现分段错误。

在SO用户的建议之后添加代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc,char **argv)
{
        char *ipfile,line[80];
        FILE *fp;
        char *field1,*field2,*field3,*field4,*field5,*field6,mystr[10];
        int row,column,weight;
        ipfile = (char *)argv[1];
        printf("The input file name is %s\n",ipfile);
        fp = fopen(ipfile,"r");

        if(fp == NULL) //Checking whether the command line argument was correctly or not.
        printf("There is no such file in the directory.\n");

        while(fgets(line,80,fp) != NULL)
        {
                field1 = strtok(line," ");
                //row    = atoi(field1);
                field2 = strtok(NULL," ");
                field3 = strtok(NULL," ");
                //column = atoi(field3);
                field4 = strtok(NULL," ");
                field5 = strtok(NULL," []=;");
                //field6 = strtok(NULL," ");

                printf("%s\n",field5);
                //printf("Row-%d Column - %d Weight - %d\n",row,column,weight);
        }
        fclose(fp);
        return 0;
}

2 个答案:

答案 0 :(得分:7)

你在空格上拆分字符串,所以这只有4个令牌,而你的代码需要6:

200 -> 103 [weight=7];

指定" []=;"作为分隔符来解决此问题。 (或者当你在它的时候也包括->。)

答案 1 :(得分:2)

试试这个:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
    char line[80];
    char *field1, *field2, *field3, *field4, *field5;
    char *ipfile = argv[1];

    if (argc == 1)
    {
        fprintf(stderr, "Usage: %s file\n", argv[0]);
        return(1);
    }
    printf("The input file name is %s\n", ipfile);
    FILE *fp = fopen(ipfile, "r");

    if (fp == NULL) //Checking whether the command line argument was correctly or not.
    {
        fprintf(stderr, "There is no such file in the directory.\n");
        return(1);
    }

    char const *delim = " [];=";
    while (fgets(line, sizeof(line), fp) != NULL)
    {
        printf("Input: %s", line);
        field1 = strtok(line, delim);
        //row    = atoi(field1);
        field2 = strtok(NULL, delim);
        field3 = strtok(NULL, delim);
        //column = atoi(field3);
        field4 = strtok(NULL, delim);
        field5 = strtok(NULL, delim);
        //field6 = strtok(NULL, delim); 
        printf("Field 1: %s\n", field1);
        printf("Field 2: %s\n", field2);
        printf("Field 3: %s\n", field3);
        printf("Field 4: %s\n", field4);
        printf("Field 5: %s\n", field5);
        //printf("Row-%d Column - %d Weight - %d\n", row, column, weight);
    }
    fclose(fp);
    return 0;
}

在您的数据上(存储在文件strtok.data中),它会产生:

The input file name is strtok.data
Input: 200 -> 103 [weight=7];
Field 1: 200
Field 2: ->
Field 3: 103
Field 4: weight
Field 5: 7
Input: 200 -> 153 [weight=27];
Field 1: 200
Field 2: ->
Field 3: 153
Field 4: weight
Field 5: 27
Input: 200 -> 53 [weight=9];
Field 1: 200
Field 2: ->
Field 3: 53
Field 4: weight
Field 5: 9
Input: 200 -> 178 [weight=43];
Field 1: 200
Field 2: ->
Field 3: 178
Field 4: weight
Field 5: 43
Input: 55 -> 2 [weight=23];
Field 1: 55
Field 2: ->
Field 3: 2
Field 4: weight
Field 5: 23
Input: 55 -> 14 [weight=50];
Field 1: 55
Field 2: ->
Field 3: 14
Field 4: weight
Field 5: 50
Input: 55 -> 20 [weight=17];
Field 1: 55
Field 2: ->
Field 3: 20
Field 4: weight
Field 5: 17
Input: 55 -> 22 [weight=1];
Field 1: 55
Field 2: ->
Field 3: 22
Field 4: weight
Field 5: 1
Input: 55 -> 74 [weight=7];
Field 1: 55
Field 2: ->
Field 3: 74
Field 4: weight
Field 5: 7
Input: 55 -> 93 [weight=9];
Field 1: 55
Field 2: ->
Field 3: 93
Field 4: weight
Field 5: 9
Input: 55 -> 122 [weight=27];
Field 1: 55
Field 2: ->
Field 3: 122
Field 4: weight
Field 5: 27
Input: 65 -> 8 [weight=27];
Field 1: 65
Field 2: ->
Field 3: 8
Field 4: weight
Field 5: 27
Input: 65 -> 9 [weight=9];
Field 1: 65
Field 2: ->
Field 3: 9
Field 4: weight
Field 5: 9

如果遇到问题,请打印。打印输入行以确保您正在阅读您认为正在阅读的内容。打印您获得的值。如果你遇到了seg故障问题,请注意打印空指针。如果你尝试打印一个空字符串,一些C库(也是)类似并打印<null>或类似的东西;其他人崩溃。你很幸运;你有一个崩溃的,所以你知道你什么时候出错了。我没有添加代码来检查strtok()的回报;如果这是我的代码而我使用的是strtok() - 这不太可能;我不喜欢strtok() - 然后我会在再次尝试之前检查每个字段是否为空。我可能会使用数组和循环而不是单独命名的变量。