0

I have a function, which has to read a file, each line into a linked list element, but it can't read the content of the file.
The function is the following:

Leaderboard *Read()
{
    FILE *fp = fopen("leaderboard.txt", "rt");
    if (fp != NULL)
    {
        Leaderboard *head = malloc(sizeof(Leaderboard));
        head->name = malloc(sizeof(char) * 21);
        head->points = 0;
        if (fscanf(fp, "%s %d\n", head->name, &head->points) == 2)
        {
            head->prev = NULL;
            head->next = NULL;
            Leaderboard *p = head;
            while (!feof(fp))
            {
                Leaderboard *newElement = malloc(sizeof(Leaderboard));
                newElement->name = malloc(sizeof(char) * 21);
                newElement->points = 0;
                fscanf(fp, "%s %d\n", newElement->name, &newElement->points);
                {
                    newElement->next = NULL;
                    newElement->prev = p;
                    p->next = newElement;
                    p = p->next;
                }
            }
            fclose(fp);
            return head;
        }
        else
        {
            perror("An error occured while reading the file.");
            fclose(fp);
            free(head->name);
            free(head);
            return NULL;
        }
    }
    else
    {
        perror("An error occured while opening the file.");
    }
    return NULL;
}

The file looks like this:
name point[int]
name2 point2[int]
...
For example
Peter 5
John 3
Bill 1
etc.

But when I try to read the file, it ruins the whole content, and I get ugly things like this:
đYgŚĹU 0
(I get this when I don't check the value of fscanf)
What is wrong with it?

UPDATE:
This is the function, which writes the new data into the file:

    void Save(Uint16 *name, int point)
{
    char* cname = UintToChar(name);
    FILE *fp = fopen("leaderboard.txt", "wt");
    if (fp == NULL)
    {
        perror("An error occured while opening the file.");
        free(cnev);
        return;
    }
    Leaderboard *head;
    head = Read();
    if (head == NULL)
    {
        fprintf(fp, "%s %d\n", cname, point);
        free(cname);
        fclose(fp);
        return;
    }
    Leaderboard *new = malloc(sizeof(Leaderboard));
    Leaderboard *p;
    new->name = cname;
    new->point = point;
    if (head->next == NULL)
    {
        new->next = NULL;
        new->prev = head;
        head->next = new;
    }
    else
    {
        p = head;
        while (p->next != NULL && p->points > point)
        {
            p = p->next;
        }
        if (p->points > point)
        {
            new->prev = p;
            new->next = p->next;
            p->next = new;
        }
        else
        {
            new->prev = p->prev;
            p->prev->next = new;
            new->next = p;
            p->prev = new;
        }
    }
    p = head;
    int i = 1;
    while (head != NULL && i <= 10)
    {
        fprintf(fp, "%s %d\n", head->name, head->points);
        p = head->next;
        free(head->name);
        free(head);
        head = p;
        i++;
    }
    free(cname);
    fclose(fp);
}
pedro
  • 83
  • 1
  • 2
  • 9
  • Please see [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Weather Vane Nov 25 '17 at 22:12
  • I tested the code on my machine but I couldn't reproduce the problem : `Leaderboard *head = Read(); while(head != NULL) { printf("%s %d\n", head->name, head->points); head = head->next; }` – Hassan Nov 25 '17 at 22:28
  • I get the error message in terminal, so it can't read the date properly. – pedro Nov 25 '17 at 22:49
  • Could you post the error message and the relevant code ? You lost me at "date". – Hassan Nov 25 '17 at 23:34
  • Sorry, I wanted to write "data". The relevant code is exactly the same that I uploaded here, and I get this message: "An error occured while reading the file." So the message in the code, in the first "else" block. – pedro Nov 25 '17 at 23:44
  • I understand now. I tried your code with the "Peter 5..." example you provided (one word and one integer per line) but it worked as expected. I'm afraid I can't see why the might behave this way. Could the input file be malformatted at some point ? – Hassan Nov 26 '17 at 00:23
  • I don't know where it could be malformatted. If it works on your computer, why can't it work on mine? Does it matter that I use arch linux? I don't think so. The text file can't be different in any way, in my opinion, on any OS. – pedro Nov 26 '17 at 01:39
  • I updated the code, you can see the function in which I write the new datas into the file. Do you think the problem is actually in this function? – pedro Nov 26 '17 at 02:01
  • `fscanf(fp, "%s...` --> `fscanf(fp, "%20s...` 2 places. – chux - Reinstate Monica Nov 26 '17 at 02:52
  • It still doesn't work. It writes 20 spaces before the name and point. – pedro Nov 26 '17 at 09:28
  • Do you use the same file in Save() ? If so, then opening it with fopen(..., "wt") will empty its contents therefore making the fscanf call in Read() return something other than 2. – Hassan Nov 26 '17 at 16:44
  • I changed the order of the lines, but it still doesn't work. Does it have a concrete reason, or this just the shit of my computer? (It would be funny with a thinkpad) – pedro Nov 26 '17 at 18:05
  • I suggest testing the Read method separately with a text file that you already wrote, just to rule out the possibility that Save is what's causing problems. Test it with the Peter... example you initially posted and see if it works on its own. – Hassan Nov 27 '17 at 20:46
  • Ok then, I will test is. Thanks for all your help guys! :) – pedro Nov 28 '17 at 11:04

0 Answers0