0

I'm in stack with C coding. I've got a problem to understand why I have a strange outpu. I try to print the information that I read from the file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct list_element
{
    char word[30];
    int l1;
    int l2;
    struct list_element *next;
} item;
typedef item *list;

int main()
{
    item a;
    list L;
    list root = NULL;
    //item root=NULL;
    FILE *f;
    if ((f = fopen("ImaFile.bin", "rb")) == NULL)
    {
        printf("can't open");
        exit(-1);
    }
    else
        puts("it's open");

    char w[30];
    int g, d;
    while (!feof(f))
    {
        fscanf(f, "%s %d %d", a.word, &a.l1, &a.l2);
        printf("word: %s, l1: %d, l2: %d", a.word, a.l1, a.l2);

        puts(" ");
        L = (list)malloc(sizeof(item));
        fscanf(f, "%s %d %d", w, &g, &d);
        printf("word: %s, l1: %d, l2: %d", w, g, d);
        //  L->word=w;
        strcpy(L->word, w);
        L->l1 = g;
        L->l2 = d;
        L->next = root;
        root = L;
        printf("word: %s, l1: %d, l2: %d", L->word, L->l1, L->l2);
    }

    fclose(f);
    return 0;
}

OUTPUT

word: , l1: 0, l2: 0word: , l1: 0, l2: 0word: , l1: 30198988, l2: 0 
word: , l1: 0, l2: 0word: , l1: 0, l2: 0word: , l1: 30198988, l2: 100663296 
word: , l1: 0, l2: 0word: , l1: 0, l2: 0word: , l1: 30198988, l2: 0 
word: , l1: 0, l2: 0word: , l1: 0, l2: 0word: , l1: 13421772, l2: 134217728 
word: , l1: 0, l2: 0word: , l1: 0, l2: 0word: , l1: 13421772, l2: 33554432 
word: , l1: 0, l2: 0word: , l1: 0, l2: 0word: , l1: 13421772, l2: 134217728 

Also, I can't understand why array of char (string) it's void in my output, and why all numbers for l1 and l2 are zero.

Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68
Ciao
  • 11
  • 5
  • 3
    https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Yunnosch Jul 21 '21 at 21:11
  • 4
    You have a binary file and expect to read strings from it? `"%s"` Seems like a contradiction. Can you provide sample content of such a file? – Yunnosch Jul 21 '21 at 21:12
  • Also, please format your code with consistent spacing and indentation to make it more readable. – kaylum Jul 21 '21 at 21:13
  • @Yunnosch in this link there aren't the answer to my question. In this link the people talk about feof. And I can't attach this file here. But my code read just the 1st word and 1st number – Ciao Jul 21 '21 at 21:28
  • @kaylum already done – Ciao Jul 21 '21 at 21:29
  • 1
    `L=(list)malloc(sizeof(item));` Casting results of `malloc()` family is [considered as a bad practice](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). Also hiding pointers via `typedef` introduces confusion. – MikeCAT Jul 21 '21 at 21:43
  • 1
    Ciao, If the return value of `fscanf(f, "%s %d %d", a.word, &a.l1, &a.l2 );` or `fscanf(f, "%s %d %d", w, &g, &d);` is not 3, stop. Fix that issue first. What is the return value? – chux - Reinstate Monica Jul 21 '21 at 21:56
  • 4
    Without knowing the *precise* form and at least a sample of valid content of a file, we're guessing. Then again, so are you. The `feof` issue is real, and shouldn't be ignored. Further, exactly *none* of your `fscanf` calls are verified as successfully parsing the number of arguments you're asking of it. I suspect if you bothered to actually do that you'd quickly narrow down your problem(s). – WhozCraig Jul 21 '21 at 21:56
  • 2
    You read an article explaining in detail why `while (!feof(f))` is always a mistake and you do not see anything relating to your question here? Hmm, ok. – Yunnosch Jul 21 '21 at 21:58
  • " And I can't attach this file here." --> With that, or a dump of its content , it is too inefficient to go forward. – chux - Reinstate Monica Jul 21 '21 at 22:01
  • @chux-ReinstateMonica the correct it's fscanf(f, "%s %d %d", w, &g, &d); Because i try to read the file and after insert into the list with { strcpy(L->word, w); L->l1=g; L->l2=d; L->next=root; root=L;} The result of fscaf it's 1. 1st output it's word: lucerna, l1: 0, l2: 0. From the second it's always "word: , l1: 0, l2: 0word", the word it's void/empty. – Ciao Jul 21 '21 at 22:09
  • 1
    Assuming your file is text and not binary, you should read only one line per iteration. So remove your first `fscanf` and `printf`, and just proceed after your call to `scanf` if it returned `3` (that is, `ret == 3`). With these changes, you won't get empty/rubbish fields anymore. Also, add a `\n` at the end of the strings you print with `printf` to make the output clearer. – Jardel Lucca Jul 21 '21 at 22:27
  • 1
    I found the error. I had to use fread, not fscanf. while(fread(&v[i], sizeof (struct list), 1, f)>0){ printf("%s %d %d ", v->word, v->l1, v->l2); } With this code I've got the correct output – Ciao Jul 21 '21 at 22:37
  • @Ciao You can post your [own answer](https://stackoverflow.com/help/self-answer) if you like. – chux - Reinstate Monica Jul 21 '21 at 23:33

0 Answers0