Sponsored Content
Full Discussion: Feof
Top Forums Programming Feof Post 11405 by primal on Tuesday 4th of December 2001 07:27:38 PM
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
 
ferror(3C)						   Standard C Library Functions 						ferror(3C)

NAME
ferror, feof, clearerr, fileno - stream status inquiries SYNOPSIS
#include <stdio.h> int ferror(FILE *stream); int feof(FILE *stream); void clearerr(FILE *stream); int fileno(FILE *stream); DESCRIPTION
The ferror() function returns a non-zero value when an error has previously occurred reading from or writing to the named stream (see Intro(3)). It returns 0 otherwise. The feof() function returns a non-zero value when EOF has previously been detected reading the named input stream. It returns 0 otherwise. The clearerr() function resets the error indicator and EOF indicator to 0 on the named stream. The fileno() function returns the integer file descriptor associated with the named stream; see open(2). ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
open(2), Intro(3), fopen(3C), stdio(3C), attributes(5), standards(5) SunOS 5.11 17 Feb 2005 ferror(3C)
All times are GMT -4. The time now is 03:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy