I'm trying to allocate memory for a new struct for each line in a file, but when the file is empty, my loop still allocates memory once. the issues is using while(!feof(file)), but I can't figure out an alternative check for the while loop.
the loop looks like this:
while(!feof(f))
{
p = (struct PlayerTime*)malloc(sizeof(struct PlayerTime));
head = p;
fscanf(f, "%f %s", &p->seconds, p->name);
p = p->next;
}
the pointers and structs are all defined prior to the loop, I just can't figure out how to get this to not loop if there's nothing in the file.
No comments:
Post a Comment