Feof


 
Thread Tools Search this Thread
Top Forums Programming Feof
# 1  
Old 12-04-2001
Question Feof

Hi Everyone,
I'm having problems with feof
I've storing my variables inside a file, and then in another program, opening that file and recalling the information.
The problem im having is that it's repeating the last set of entries in my while loop.
(if i have one set of product no., quantity, and price in my file, it repeats it twice. if i have two sets or more in my file, it repeats the last set twice)
I think it has something to do with how i have my while(!feof(file)) set up.
Does anyone know what's going on?
Code:
#include[stdio.h]
FILE *invfile;
int prod, qty;
float price;

void main()
    {
     invfile = fopen("file.dat","r");
     while(!feof(invfile))
       {
       fscanf(invfile,"%d %d %f", &prod, &qty, &price);
       printf("Product No.     = %d\n",prod);
       printf("Quantity        = %d\n",qty); 
       printf("Price           = %.2f\n\n",price);
       }
     fclose(invfile);
    }

Thanks!
primal
# 2  
Old 12-05-2001
Hi!

Replace your code with:

#include [stdio.h]
FILE *invfile;
int prod, qty;
float price;

void main()
{
invfile = fopen("file.dat","r");
while(fscanf(invfile,"%d %d %f", &prod, &qty, &price) != EOF)
{
printf("Product No. = %d\n",prod);
printf("Quantity = %d\n",qty);
printf("Price = %.2f\n\n",price);
}
fclose(invfile);
}

Rgds
SHAIK
shaik786
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question