简体   繁体   中英

How to use fgets and sscanf to store data into structure, from text file

What i want to do is to take a text file and store the 4 lines into my structure, using a loop with fgets and sscanf to retrieve the data.

The structure is this:

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #define SIZE 16
 #define N 4

struct Prova
{
    char nome[SIZE];
    int num;
};

Now my function looks like this:

void get_str(struct Prova * ptr){

FILE *f;
int i = 0;

 if(!(f = fopen(PATH,"r+")))
  {
    perror("Errore in apertura");
    exit(0);
  }

void * buffer = malloc(sizeof(struct Prova ));

  while(fgets(buffer,sizeof(struct Prova), f))
   {
    if(sscanf(buffer, "%s %d", (ptr+i)->nome, &(ptr+i)->num) > 0)
      {i++;}
   }

   free(buffer);
   fclose(f);
  }

UPDATED: Now the function works, thanks everybody.

Main function is this:

  int main()
  {
    struct Prova * ptr = malloc(N*sizeof(struct Prova));

    get_str(ptr);

    for(int i = 0; i< N; i++)
    {
      printf("\n%s %d", (ptr+i)->nome, (ptr+i)->num);
    }
   return 0;
  }

The text file i'm trying to read is this:

Pippo 4
Paperino 1
Topolino 3
Zio_paperone 2

As suggested by others in comments check the return value of sscanf() . Also I'm not sure what argument you are passing to get_str() .

Also In void * buffer = malloc(N*sizeof(struct Prova )); you no need to allocate memory for N structure because using fgets() you are overwriting data every time. It should be void * buffer = malloc(2*sizeof(struct Prova ));

For particular case you mention, it might be the solution

while(fgets(buffer,sizeof(struct Prova), f)) {
       int ret = sscanf(buffer, "%s %d", (ptr+i)->nome, &(ptr+i)->num);
       printf("ret = %d\n",ret);
       printf("\n%s %d", (ptr+i)->nome, (ptr+i)->num);
       i++;
}

Here is the complete code

#define N 4
struct Prova {
        char nome[SIZE];
        int num;
};
void get_str(struct Prova * ptr) {
        FILE *f;
        if(!(f = fopen("data","r+"))) {
                perror("Error");
                exit(0);
        }
        void * buffer = malloc(2*sizeof(struct Prova ));/* Allocate 2* size, by considering Newline char & possible padding */ 
        int i =0;
        while(fgets(buffer,sizeof(struct Prova), f)) {
                int ret = sscanf(buffer, "%s %d", (ptr+i)->nome, &(ptr+i)->num);
                printf("ret = %d\n",ret);
                i++;
        }
        free(buffer);
        fclose(f);
}
int main() {
        struct Prova *v = malloc(N*sizeof(struct Prova));/* here you need to allocate 
                                for N structure, amke sure file have N lines of same format */
        get_str(v);
        for(int i =0;i<N;i++) {
                printf("\n%s %d\n",(v+i)->nome,(v+i)->num);
        }
        return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM