Ошибка сегментации при поиске данных в текстовом файле (C) - PullRequest
0 голосов
/ 11 апреля 2020

У меня есть следующие функции:

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);
  }
}

Этот код извлекает некоторые данные из следующего текстового файла:

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. Пожалуйста, кто-нибудь может помочь мне найти мою ошибку.

1 Ответ

0 голосов
/ 11 апреля 2020

Это было нулевое значение. Я преобразовывал строку (ноль) в целое число, и именно поэтому произошла ошибка.

Спасибо всем!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...