0

I really need some help to figure out what's the problem with the reading from file of a nested lists. Briefly, these are the nested lists:

   typedef struct car* parcheggio;

   struct car{
     char* targa;
     char* nome;
     char* cognome;
     parcheggio next;
   };

   typedef struct data* calendario;

   struct data{
     char* data;
     parcheggio aut;
     calendario next;
   };

And with the following function, passing the path of the txt file where all the dates are saved, I'm able to store dates from file into list:

calendario get_datefromFile(char folder[100]){
  FILE *fp1;
  calendario temp, temp1;
  char flag;

  fp1 = fopen(folder, "r");
  temp = (calendario)malloc(sizeof(struct data));
  temp->data = malloc(10*sizeof(char));
  fscanf(fp1, "%s", temp->data);

  temp1 = temp;

  while(flag != EOF){
    temp1->next = (calendario)malloc(sizeof(struct data));
    temp1 = temp1->next;
    temp1->data = malloc(10*sizeof(char));
    fscanf(fp1, "%s", temp1->data);

    flag = getc(fp1);
  }
  temp1->next = NULL;
  fclose(fp1);
  return temp;
  }

Then I have other files, every file contains the info of struct car( targa nome and cognome) of a specific date, and obviously I want to match each calendario->data with data->aut and every data->aut->targa, ...->nome, ...->cognome. So to create a complete list of every date.

And I tried to do so through these functions:

calendario getcarall(calendario head1){
  parcheggio be;
  calendario temp = head1;
  char dest[100] = "/home/utente/Documenti/Progetto/store/";
  char path[100];
  strcpy(path, dest);

  while(temp != NULL){
    strcat(dest, head1->data);
    be = NULL;
    be = car_filetolist(dest);
    head1->aut = be;

    strcpy(dest, path);
    head1 = head1->next;
  }
  return temp;
}

parcheggio car_filetolist(char dest[100]){
  FILE *file;
  char flag;
  parcheggio head, current;

  file = fopen(dest, "r");

  head = (parcheggio)malloc(sizeof(struct car));
  head->targa = malloc(10*sizeof(char));
  fscanf(file, "%s", head->targa);
  head->nome = malloc(12*sizeof(char));
  fscanf(file, "%s", head->nome);
  head->cognome = malloc(12*sizeof(char));
  fscanf(file, "%s", head->cognome);

  current = head;

  while(flag != EOF){
    current->next = (parcheggio)malloc(sizeof(struct car));
    current = current->next;
    current->targa = malloc(10*sizeof(char));
    fscanf(file, "%s", current->targa);
    current->nome = malloc(12*sizeof(char));
    fscanf(file, "%s", current->nome);
    current->cognome = malloc(12*sizeof(char));
    fscanf(file, "%s", current->cognome);

    flag = getc(file);
  }
  current->next = NULL;

  fclose(file);

  return head;
  }

But gcc returns segmentation fault when program enter in func car_filetolist.

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 1
    1. You should check if the return value of `fopen()` is not `NULL`. 2. The return value of [`getc()`](https://man7.org/linux/man-pages/man3/getc.3p.html) should be assigned to `int`, not `char`. 3. Casting results of `malloc()` family is [considered as a bad practice](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – MikeCAT Jun 15 '21 at 11:08
  • 1
    In `get_datefromFile`, the value of `flag` is used before it has been initialized. And it should be declared as `int`, not `char`. – Ian Abbott Jun 15 '21 at 11:08
  • In `getcarall`, the string in `dest[100]` grows every time around the loop. Perhaps it overruns? I suspect that you really wanted to append to the same place in `data[100]` each time around the loop. – Ian Abbott Jun 15 '21 at 11:14
  • 1
    `car_filetolist` *also* uses `flag` before it is initialized, and it should be declared `int`. – Ian Abbott Jun 15 '21 at 11:16
  • 2
    [Stop casting `malloc` in C programs](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). Verify your IO operations, lest you violate [Spencer's Sixth Commandment](https://www.seebs.net/c/10com.html). Since *none* of your string member lengths are actually dynamic (ex: `malloc(10*sizeof(char))`, you'll find most of this easier if your members were just fixed ex:`char[10]` buffers. Finally, hiding pointer types in typedef aliases is obfuscation, not helpful, in all but two scenarios (neither of which are applicable here). do yourself a favor and don't make it a habit. – WhozCraig Jun 15 '21 at 11:16
  • 1
    Always check if `fopen` fails. The file you try to open might not exist. – Jabberwocky Jun 15 '21 at 11:28
  • Thank you, i fixed the code as you suggested. I figured out what is the issue that makes gcc returns segm fault. Program can't find the specific date file using strcat to pass the right path. – Alessandro Romeo Jun 15 '21 at 12:37
  • OT: regarding: `typedef struct car* parcheggio;` and similar statements: typedef'ing a pointer is a very bad idea. It leads to confusion later in the code. – user3629249 Jun 17 '21 at 15:25

0 Answers0