我试图逐行读取配置文件,然后标记并将结果存储到单独的变量中。我的配置文件如下

stage 1
num_nodes 2
nonce 234567

我需要分别标记行中的每个值,例如,在第一行“stage”中,用于检查是否已从配置文件中读取stage值,然后将其值保存在变量中。我的标记化似乎是正确的。但是,当我尝试在标记化后操作变量时,它会给我一个分段错误。最多我只能成功地操作其中一个变量,即stage或num_nodes或nonce,但不能组合它们。即使尝试做一些类似的事情
stage = stage + 1;
num_nodes = num_nodes + 1;

但是,如果我只对一个变量进行更改,这会导致分段错误,例如:
num_nodes = num_nodes + 1;

那就行了。我正在粘贴下面的代码,请告诉我这里缺少什么。
main(int argc, char *argv[]){
  int nonce;
  int num_nodes;
  int stage;
  char filename[256];
  char *token1, *token2, *str;
  FILE* fp;
  char bufr[MAXLINE];

  printf("Please enter config file name\n");
  scanf("%s",filename);
  printf("You entered %s\n", filename);

  if((fp = fopen(filename, "r")) != NULL){

        while(fgets(bufr, MAXLINE, fp) != NULL){
            if(bufr[0] == '#') // to skip comments
                continue;

            printf("This is bufr: %s",  bufr);
            str = bufr;

              for(str;  ;str = NULL){
                token1 = strtok(str, " ");

                if(strcmp(token2, "num_nodes") == 0){
                    num_nodes = atoi(token1);
                    printf("num_nodes = %d\n", num_nodes);
                }

                if(strcmp(token2, "nonce") == 0){
                    nonce = atoi(token1);
                    printf("nonce = %d\n", nonce);
                }

                if(strcmp(token2, "stage") == 0){
                    stage = atoi(token1);
                    printf("stage = %d\n", stage);
                }

                token2 = token1; // making a copy of pointer

                if(str == NULL){
                    break;
                }
          }//end of for loop

        }//end of while loop
        fclose(fp); //close the file handle
    }
    else{
        printf("failed, file not found!\n");
    }

/*      This is where the segmentation fault kicks in, try to uncomment two lines and it will give a segmentation fault, if uncomment just one, then it works fine.
    nonce = nonce + 2;
    num_nodes = num_nodes + 1;
    printf("stage = %d\n", stage);
*/
}

最佳答案

您的代码包含:

token1 = strtok(str, " ");

if (strcmp(token2, "num_nodes") == 0){
    num_nodes = atoi(token1);
    printf("num_nodes = %d\n", num_nodes);
}

您刚刚设置了token1,但要继续比较token2?这可能会导致堆芯转储,至少在从未设置token2的第一次转储时是这样。
最后,在循环之后,出现核心转储的唯一原因是,您在分配的内存边界之外到处乱跑。现在还不清楚为什么,但是循环结构是……很奇怪,我们可以这么说。
这是一个清理,不是崩溃为我你的代码版本。你的原作还不错,但不确定的状态令人担忧。输出的一个版本包含以下信息:
Please enter config file name
You entered config.file
This is bufr: # Comment
This is bufr:
This is bufr: stage 1
token1 = <<stage>>; token2 = <<>>
token1 = <<1
>>; token2 = <<stage>>
stage = 1
This is bufr: num_nodes 2
token1 = <<num_nodes>>; token2 = <<des>>
token1 = <<2
>>; token2 = <<num_nodes>>
num_nodes = 2
This is bufr: nonce 234567
token1 = <<nonce>>; token2 = <<67
>>
token1 = <<234567
>>; token2 = <<nonce>>
nonce = 234567
This is bufr:
stage = 1

注意token2中残留的碎片。我在下面的代码中进一步清理了它:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum { MAXLINE = 4096 };

int main(void)
{
    int nonce = -1;
    int num_nodes = -1;
    int stage = -1;
    char filename[256];
    char *token1, *token2, *str;
    FILE *fp;
    char bufr[MAXLINE];

    printf("Please enter config file name\n");
    scanf("%s", filename);
    printf("You entered %s\n", filename);

    if ((fp = fopen(filename, "r")) == NULL)
    {
        printf("failed, file not found!\n");
        return(1);
    }

    while (fgets(bufr, MAXLINE, fp) != NULL)
    {
        printf("This is bufr: %s", bufr);
        if (bufr[0] == '#' || bufr[0] == '\n')
            continue;

        token2 = "";
        for (str = bufr; (token1 = strtok(str, " \n\t")) != 0; str = NULL)
        {
            printf("token1 = <<%s>>; token2 = <<%s>>\n", token1, token2);
            if (strcmp(token2, "num_nodes") == 0) {
                num_nodes = atoi(token1);
                printf("num_nodes = %d\n", num_nodes);
            }
            if (strcmp(token2, "nonce") == 0) {
                nonce = atoi(token1);
                printf("nonce = %d\n", nonce);
            }
            if (strcmp(token2, "stage") == 0) {
                stage = atoi(token1);
                printf("stage = %d\n", stage);
            }

            token2 = token1;

            if (str == NULL)    /* Terminate after name/value */
                break;
        }

    }
    fclose(fp);

    nonce = nonce + 2;
    num_nodes = num_nodes + 1;
    printf("stage = %d\n", stage);
    printf("nonce = %d\n", nonce);
    printf("nodes = %d\n", num_nodes);

    return(0);
}

此代码在Mac OS X 10.8.5和GCC 4.8.1上使用命令行进行干净编译:
gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition cfg.c -o cfg

给定一个名为token2的输入文件:
# Comment

stage 1
num_nodes 2
nonce 234567
 

(结尾有一个空行),输出是:
Please enter config file name
You entered config.file
This is bufr: # Comment
This is bufr:
This is bufr: stage 1
token1 = <<stage>>; token2 = <<>>
token1 = <<1>>; token2 = <<stage>>
stage = 1
This is bufr: num_nodes 2
token1 = <<num_nodes>>; token2 = <<>>
token1 = <<2>>; token2 = <<num_nodes>>
num_nodes = 2
This is bufr: nonce 234567
token1 = <<nonce>>; token2 = <<>>
token1 = <<234567>>; token2 = <<nonce>>
nonce = 234567
This is bufr:
stage = 1
nonce = 234569
nodes = 3

关于c - 逐行读取文件并在C中使用strtok(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18939449/

10-11 22:06
查看更多