Reading from a File in C , problems with fscanf functions

64 views Asked by At

My program is supposed to read from a file and fill some structs that are like post from a social network , and then fill a Queue structure with the info from the file. But the main problem seems to be the reading from file function that is posted below:

int load_user_from_file(struct Coda* Bacheca,char* nome_file){
    
    FILE* fp=fopen(nome_file,"r");
    
    if(fp == NULL){
        printf("errore lettura file");
        return -1;
        
    }else{
        printf("\nFile in lettura...");
    }
    
    int len=64;
    char tmp_msg[len];
    int tmp_n_like;
    char* buffer ;
    
    while(fgets(tmp_msg,len,fp) != NULL  ){
            
                fscanf(fp,"%s",buffer);
        tmp_n_like=getw(fp);
        
        printf("Eureka\n");
        
        printf("%s %d\n",tmp_msg,tmp_n_like);
        
        struct Post* new_post = generate_Post(tmp_msg,tmp_n_like); 
        insert_in_queue(Bacheca,*new_post);
    
    }
    
    return 0;
}

(sorry for formatting errors , i am quite a noob on the forum =( )

and the .txt file looks like this (sorry for it being in Italian):

enter image description here

The first string being the msg of the post and below there is the number of likes.

I tried various methods to get the string and the integer below to fill all the posts. I read other posts about this but still can't figure it out , sorry if I had to make another post. Sometimes the number is incorrect or it does not read anything. I know it's a problem with buffers and in particular the spaces in the string but i really can't figure it out. I am quite close now with this as i get this :

enter image description here

It looks like it gets the first number of the date of post for the integer of the likes then he adds more post the needed using both the integer as the msg and as the number of likes.

How should i fix this? Much help needed here XP

0

There are 0 answers