Trying to read from txt file until a semicolon and will store it into a array inside struct.
struct Total
{
char *Name;
int *No;
}MyCities;
This is my struct made the arrays pointers so i can allocate memory later. Depending on the content of the txt. Each line is a entry for the struct.
London;15
Oslo;12
vienna;35
Let's say this is the content of txt file, it will read the city name into MyCities.Name and the number after the semicolon into MyCities.No
FILE *fp;
char line;
fp = fopen("City.txt", "r");
for (line= getc(fp); line!= EOF; line= getc(fp)) //for getting number of lines
{
if (line == '\n')
count++;
}//this is for counting how many lines in txt
MyCities.No = malloc( count * sizeof *MyCities.No );
if (MyCities.No == NULL) {
fprintf(stderr, "Malloc failed.\n");
exit(1);
}
MyCities.Name = malloc( count * sizeof *MyCities.Name );
if (MyCities.Name == NULL) {
fprintf(stderr, "Malloc failed.\n");
exit(1);
}
So at this point I have no idea how should I proceed.