0

I want to take the lines of a text file (.txt) and store it with a list (structure that I created). Something in the compilation really makes me perplex and don't have sense for me.

TList* names = initList();

fp = fopen(filename, "r");
if (fp == NULL)
    exit(EXIT_FAILURE);


while ((read = getline(&line, &len, fp)) != -1) {
    append(names, line);
    printf("last -> %s\n", (const char*)names->last->content);
}

printf("%s", (const char*)names->last->content);

This instruction take the line and displays it :

printf("last -> %s\n", (const char*)names->last->content);

but not that one :

printf("%s", (const char*)names->last->content);

I don't understand because nothing have changed between these instructions. Moreover, I have tested the TList structure with char* and it was working.

list.c :

TElement* initElement(const void* content){
    TElement* element = (TElement*)malloc(sizeof(TElement));
    element->content = content;
    element->prev = NULL;
    element->next = NULL;
    return element;
}

TList* initList(){
    TList* list = (TList*)malloc(sizeof(TList));
    list->first = NULL;
    list->last = NULL;
    list->size = 0;
    return list;
}


void append(TList* list, const void* content){
    TElement* element = initElement(content);
    if (list->size == 0){
        list->first = element;
        list->last = element;
    }
    else {
        list->last->next = element;
        element->prev = list->last;
        list->last = element; 
    }
    list->size += 1;
}

list.h :

typedef struct TElement{
    const void* content;
    struct TElement* prev;
    struct TElement* next;
} TElement;

typedef struct TList{
    int size;
    TElement* first;
    TElement* last;
} TList;

TElement* initElement(const void* content);
TList* initList();
void append(TList* list, const void* content);
Witzig Adrien
  • 102
  • 12
  • 1
    Call `fflush()` after `printf()` or append a newline character in the call to `printf()`. Sidenote: You don't need to cast the `void *` to `const char *`; there's an implicit conversion here. See: https://stackoverflow.com/q/9317529/20017547 – Harith May 25 '23 at 19:36
  • Alternatively, you can call `puts()`. – Harith May 25 '23 at 19:39
  • @Haris i have called `fflush(fp)` and it doesn't change anything, altough it was an error from me :) – Witzig Adrien May 25 '23 at 19:39
  • Don't you reckon 'tis `stdout` that ought to be flushed? ;) – Harith May 25 '23 at 19:40
  • @Haris in the `while()` ? – Witzig Adrien May 25 '23 at 19:42
  • 1
    Aside: Unless you're compiling as C++, remove the casts from `malloc()`. `malloc()` and family returns a generic `void *` type that is implicitly converted to any other pointer type, as mentioned earlier. – Harith May 25 '23 at 19:42
  • After the `while()`, right after the call to `printf()`. See the linked answer for why. – Harith May 25 '23 at 19:44
  • @Haris It printed me a blank line – Witzig Adrien May 25 '23 at 19:45
  • 3
    `getline()` doesn't allocate a new `line` if the existing buffer is big enough. So you may be using the same `line` pointer in each element of `TList`, and the contents get overwritten each time you get a line. Do `append(names, strdup(line))` – Barmar May 25 '23 at 20:12
  • 2
    ... and in the event that the line is *not* big enough, `getline()` reallocates the space to which it points, which invalidates any copies of that pointer you previously stored in your list. – John Bollinger May 25 '23 at 20:43

0 Answers0