Reputation: 31
struct Country{
char name[50];
float square;
float quantityOfPeople;
};
while (fread(&newRecord,sizeof(struct Country),1,fout)) {
printf("hello\n");
if (strcmp (countryName, newRecord.name) == 0) {
printf("A record with requested name found and deleted.\n\n");
found=1;
} else {
fwrite(&newRecord, sizeof(struct Country), 1, fp_tmp);
}
}
my file is
hjk
78.699997
799.900024
america
3432.300049
2323.199951
ghi
78.900002
89.000000
dmdmd
746234052608.000000
0.000000
In my file there is 4 records of country so it should print 4 times hellp, but it prints hello twice. The main task is to remove the duplicate entry in the file.
Upvotes: 1
Views: 63
Reputation: 154492
fread(&newRecord,sizeof(struct Country),1,fout)
is for reading binary data from a file.
OP's file is text based.
How can i read struct from a file?
Open the file in text mode.
Form a helper function that reads 4 lines with 4x fgets()
and then processes the lines for 1) country name, 2) some double
(see strod()
) 3) another double
and 4) an optional empty line.
If all these pass, save in a struct Country
Return 1 for success, 0 for failure, EOF
for nothing read.
Upvotes: 1