reading binary file and save into an list

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.



Read more here: https://stackoverflow.com/questions/68476394/reading-binary-file-and-save-into-an-list

Content Attribution

This content was originally published by Ciao at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: