I am trying to read data from a file and save it to a struct and print it to a screen. Everything works but it print and extra line of characters . I tried using malloc but I don't think I am using it correctly. Here is the code.
void SearchItem(struct grocery NewList) {
struct grocery NList;
char sitem[50];
NList.list = fopen("grocerylist.txt", "r");
if (NList.list == NULL) {
printf("File could not be opened. \n");
} else {
fseek(NList.list, 0, SEEK_END);
int sz = ftell(NList.list);
rewind(NList.list);
char *Data = (char *)malloc(sz + 1);
for (int i = 0; i < strlen(Data); i++) {
fread(&NList, sizeof(struct grocery), 1, NList.list);
if (NList.item_name != NULL) {
printf("\n %s %.2f %d", NList.item_name,
NList.unit_price, NList.quantity);
} else {
printf("Item unavailable.\n");
}
}
}
fclose(NList.list);
}
Here is the output from file
and here is the contents of the file
How do I Fix This