Sunday, 1 April 2018

c - How do I loop through each line of a file?

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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...