【问题标题】:Segmentation fault while searching data in a txt file (C)在 txt 文件中搜索数据时出现分段错误 (C)
【发布时间】:2020-07-24 04:41:39
【问题描述】:

我有以下功能:

void find(void) {
  nd = (struct node*)malloc(sizeof(struct node) * 14);

  FILE *openedfile;

  openedfile = fopen("NSFNet.txt", "r");

  if (openedfile == NULL) {
    printf("Error, file no existente");   
    exit(1);
  } 

  fseek(openedfile, 175, SEEK_SET);
  char linea[300];
  char *aux;
  fgets(linea, 300, openedfile); 
  char *checker ="0";
  int counter = 0;
  int i = 0;

  while (fgets(linea, 300,openedfile) != NULL) {
    aux = strtok(linea, "[]"); 

    int value = atoi(aux);
    if (value == i) {
      counter++;
    }
    if (value != i) {
      nd[i].links = counter;
      printf("El contador es:\n");
      printf("%d\n", i);
      printf("%d\n", nd[i].links);
      counter = 1;
      i++;    
    }
  }
}

还有下一个:

void test(void) {
  nd = (struct node*)malloc(sizeof(struct node) * 14);

  FILE *openedfile;
  openedfile = fopen("NSFNet.txt", "r");

  if (openedfile == NULL) {
    printf("Error, file no existente");   
    exit(1);
  } 

  fseek(openedfile, 175, SEEK_SET);

  char linea[300];
  char *aux;
  fgets(linea, 300, openedfile); 
  char *checker ="0";
  int counter = 0;
  int i = 0;

  while (fgets(linea, 300,openedfile) != NULL) {
    aux = strtok(linea, "]"); 
    aux = strtok(NULL, "]");
    aux = strtok(NULL, "\t");

    printf("%s \n",aux);
  }
}

此代码从以下 txt 文件中提取一些数据:

Number of nodes: 14
Number of links: 42
==================================================
source  dest.   hops    path (link ids)
==================================================
[0] [1] 1   0
[0] [2] 1   2
[0] [3] 2   0-8
[0] [4] 3   0-8-12
[0] [5] 2   2-10
[0] [6] 4   0-8-12-18
[0] [7] 1   4
[0] [8] 2   4-26
[0] [9] 3   4-26-28
[0] [10]    3   0-8-14

第一个代码提取第一列的数字,第二个代码提取第三列的数字。这两个功能分别工作正常,但我想创造一个条件。我将两个函数混合为一个函数:

void find2(void) {
  nd = (struct node*)malloc(sizeof(struct node) * 14);

  FILE *openedfile;

  openedfile = fopen("NSFNet.txt", "r");

  if (openedfile == NULL) {
    printf("Error, file no existente");   
    exit(1);
  } 

  fseek(openedfile, 175, SEEK_SET);

  char linea[300];
  char *aux;
  char *aux2;
  fgets(linea, 300, openedfile); 
  char *checker ="0";
  int counter = 0;
  int i = 0;

  while (fgets(linea, 300,openedfile) != NULL) {
    aux2 = strtok(linea, "]"); 

    aux2 = strtok(NULL, "]");
    aux2 = strtok(NULL, "\t");

    int value = atoi(aux);
    int hops = atoi(aux2);

    if (value == i && hops == 1) {
      counter++;
    }
    if (value != i) {
      nd[i].links = counter;
      printf("El contador es:\n");
      printf("%d\n", i);
      printf("%d\n", nd[i].links);
      counter = 1;
      i++;    
    }
  }
} 

混合函数出现如下错误:exited, segmentation fault. 拜托,谁能帮我找出我的错误。

【问题讨论】:

  • 在调用strtok 并在将其传递给atoiprintf 之前,您没有检查aux 是否为NULL,这似乎有点过于自信。
  • 在尝试将其用作索引之前,您还应该检查 i < 14
  • int value=atoi(aux); 然而,aux 从未被初始化。这会导致您的分段违规并中止。
  • 在调试器中单步调试您的代码并观察它的执行情况。其次,在解析文本文件时,根据字符数执行搜索是不好的做法。即不要这样做:fseek(openedfile,175, SEEK_SET); 最好阅读 5 行并跳过它们。您对标头始终是相同字节数的假设可能会在某个时间点中断。

标签: c file text struct segmentation-fault


【解决方案1】:

这是一个空值。我正在将字符串(null)转换为整数,这就是发生错误的原因。

谢谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 2017-06-16
    • 2020-11-06
    • 2022-10-04
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    相关资源
    最近更新 更多