0

How do I create a list with numbers read from a file? I can't seem to understand how to do that, because I don't know how many numbers the sequence contains, so how am I supposed to know how many nodes my list has to be composed of. Here is the code I wrote: does the fgets command create a full list or it just fills the first node?

    #include <stdio.h>
    #include <stdlib.h>

    int Sequenza (fp f1, fp f2, fp f3);

    typedef FILE* fp;

    typedef struct Nd{
      int dato;
      struct Nd* next;
    } Nodo;

    typedef Nodo* lista;


    int Sequenza (fp f1, fp f2, fp f3){
      lista seq1, seq2;

      seq1 = malloc (sizeof(Nodo));
      seq2 = malloc (sizeof(Nodo));

      f1 = fopen ("sequenza1.txt", "r");
      f2 = fopen ("sequenza2.txt", "r");

    if (f1 == NULL || f2 == NULL){
      printf ("Errore nell'apertura di uno dei due file\n");
      return 0;
    } else {
      while (!feof(f1) && !feof(f2)){
        fgets (seq1, MAX, f1);
        fgets (seq2, MAX, f2);
      }

    }

    fclose (f1);
    fclose (f2);

   }

I'm not sure that fgets creates a full list, but just fills in the first node.

Tania
  • 1
  • 2
  • 2
    Quote: "I'm not sure that fgets creates a full list, but just fills in the first node." It's not doing any of those two. It's reading a line from a file. – Support Ukraine Dec 30 '22 at 08:13
  • See an example in my answer of how a linked list is created (except it's using user input instead of the file). The concept is the same though: https://stackoverflow.com/questions/74956170/keeping-struct-variable-in-storage-in-loop/74956377#74956377 – TheNomad Dec 30 '22 at 08:18
  • `fgets()` is for text input... The 's' kinda implies "string". You want to use `fread()` to load binary data... Don't use the pointers that are reloaded!!! They may have been written from a previous instantiation of the executable and are now meaningless. UB is bad... – Fe2O3 Dec 30 '22 at 08:21
  • 2
    I'll recommend that you take a step back and start out with something simpler. Task A: Write a program that can read numbers from a file and print them. Task B: Write a program that can add fixed, hard coded numbers to a liked list and print the list. Task C: Combine the code from A and B – Support Ukraine Dec 30 '22 at 08:22
  • @Fe2O3 It's not stated that the file is binary. The `fopen` indicates that it isn't. – Support Ukraine Dec 30 '22 at 08:24
  • @SupportUkraine The payload defined in the node struct is binary... That was my clue... There's a lot missing here and a lot that doesn't line up with other stuff... Waiting for the "don't use feof()` crowd to howl, too... Cheers... – Fe2O3 Dec 30 '22 at 08:26
  • @SupportUkraine I use Windows (not exactly a POSIX system), but have seen that the "b" specification to `fopen()` seems to be _optional_ (unless you're running Windows... like me...) Not sure what to make of that... – Fe2O3 Dec 30 '22 at 08:32
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Dec 30 '22 at 08:37
  • @Fe2O3 I'm not sure what you mean by: "The payload defined in the node struct is binary..." To me the "payload" seems to be `int dato`. But that doesn't impact whether the file contains integers in binary or text format – Support Ukraine Dec 30 '22 at 08:38
  • @SupportUkraine We should leave off and let the OP clarify their intent. The "read" function is shown as `fgets()` but the destination buffer passed to `fgets()` is a block of heap freshly allocated and looking like a node comprised of binary data... We're debating the same thing but approaching the issue from opposite perspectives; that's all... Cheers and (soon) Happy New Year... – Fe2O3 Dec 30 '22 at 08:42
  • @Tania As said in a comment above. Start with something simpler. Here https://ideone.com/ZQO6tG is a very simple example of a linked list. It's not a full implementation. It can only add to the front of the list and print the list. But you can use it as a starting point. Add functions to "add to end of list", "free the whole list", "delete front element", "delete end element", etc.... as you need. Once you have a complete functional linked list program, you can start working on the "reading from file part". Have fun. – Support Ukraine Dec 30 '22 at 08:49
  • Pick up a good book. It would benefit you. – Harith Dec 30 '22 at 10:06
  • @Tania "with numbers read from a file?" --> post an example of file contents to clarify the data organization. – chux - Reinstate Monica Dec 30 '22 at 13:53
  • @chux-ReinstateMonica s1= {0 1 3 4 5 5 6 7 7 7 7 8 8} and s2 = {3 3 3 3 3 3 3 4 5 9} – Tania Dec 30 '22 at 14:34

0 Answers0