I have a file which contains information about films like this:
Film code
Name
Year of release
Movie length(in minutes)
The film producer
I have to read this info from a file and store that info into pointers. My code so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct filmiab
{
int koodpk;
char *nimed;
int aasta;
int kestus;
char *rezi;
} filmiab;
int main()
{
filmiab *db;
FILE *f1;
f1 = fopen("filmid.txt", "r");
db->nimed = (char*)malloc(sizeof(db->nimed) * sizeof(char));
db->rezi = (char*)malloc(sizeof(db->rezi) * sizeof(char));
while(1)
{
fscanf(f1, "%d ", &db->koodpk);
fgets(db->nimed, 100, f1);
db->nimed = (char*)realloc(db->nimed, sizeof(char) * sizeof(db->nimed)); //gets more memory to store the strings
fscanf(f1, "%d %d ", &db->aasta, &db->kestus);
fgets(db->rezi, 100, f1);
db->rezi = (char*)realloc(db->rezi, sizeof(char) * sizeof(db->rezi));
printf("Filmi kood: %d\nFilmi nimi: %sAasta: %d\nKestus minutites: %d\nFilmi rezis66r: %s\n",
db->koodpk, db->nimed, db->aasta, db->kestus, db->rezi);
printf("\n");
}
return 0;
}
It just goes into an infinte loop and only prints the last 5 lines. I know that when using fgets it replaces all the strings with the last 5 lines. But what can I do so it would store all the info and that so I could print them out (or just use them) in another function. And why does it go into an infinite loop ?
EDIT: I have to use only the pointers that are created in the struct.
EDIT2: Now both these lines fgets(db->nimed, 100, f1); fgets(db->rezi, 100, f1); store the required info and the blank spaces. What to do so it only stores the names of the films and the producers.