0

我没有明显的原因得到分段错误。我正在使用该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;
}

来自评论:

当我尝试打印field1, field2, 时field3field4它们正在被打印。但是当我尝试field5并且field6我的程序出现分段错误时。

在 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;
}
4

2 回答 2

7

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

200 -> 103 [weight=7];

指定" []=;"为分隔符以解决此问题。(或者->在您使用时也包括在内。)

于 2012-12-01T22:52:16.963 回答
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

如果遇到问题,请打印内容。打印输入行以确保您正在阅读您认为正在阅读的内容。打印你得到的值。如果您遇到段错误问题,请小心打印空指针。<null>如果您尝试打印空字符串,一些 C 库(非常)和 print或类似的东西;其他人崩溃。你很幸运;你有一个崩溃的,所以当你做错了你就会知道。我没有添加代码来检查来自strtok(); 如果这是我的代码并且我正在使用strtok()- 这不太可能;我不喜欢strtok()——然后我会在重试之前检查每个字段是否不为空。我可能也会使用数组和循环而不是单独命名的变量。

于 2012-12-01T23:21:24.850 回答