Let's assume that we have file which contains:
1 John
2 Alex
3 Michael
We can get one line using fscanf()
function, but how to save it to below structure:
typedef struct entry {
int n;
char *name;
} entry_t;
I want to create the array of structures and save values from file to it, and do it dynamically. I've tried doing this in that way
entry_t *prt = malloc ( size * sizof(entry_t) );
//opening file
prt[0].name = malloc ( sizeof("John") );
fscanf (fp,"%d %s", prt[0].n, prt[0].name);
Ok, it works, but how to allocate memory for every name before get it from text file? I decided to use array of structures, because I'll use it to implement hash table.