C语言 无明显原因发生的分段错误



我无缘无故地得到了分割错误。我正在使用 strtok 函数并将每行拆分为多个标记并将它们存储在字符指针中。我的数据如下所示:

输入:

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 %sn",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("%sn",field5);
                //printf("Row-%d Column - %d Weight - %dn",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 %sn",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("%sn",field5);
                //printf("Row-%d Column - %d Weight - %dn",row,column,weight);
        }
        fclose(fp);
        return 0;
}

您正在将字符串拆分为空格,因此它只有 4 个标记,而您的代码需要 6 个:

200 -> 103 [weight=7];

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

试试这个:

#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 filen", argv[0]);
        return(1);
    }
    printf("The input file name is %sn", 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: %sn", field1);
        printf("Field 2: %sn", field2);
        printf("Field 3: %sn", field3);
        printf("Field 4: %sn", field4);
        printf("Field 5: %sn", field5);
        //printf("Row-%d Column - %d Weight - %dn", 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() - 然后我会在重试之前检查每个字段是否不为空。 我可能也会使用数组和循环,而不是单独命名的变量。

相关内容

  • 没有找到相关文章

最新更新