Instead of using fgets() within the while condition, you should use it within the brackets and use feof() for the condition.
ex:
Code:
while( fgets() )
{
// ...
}
->
Code:
while( !feof() ) // the ! is making sure it's not the end of the file
{
fgets()
// ...
}
__________________