Tuesday, June 25, 2019

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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...