A basic feof question.

A basic feof question.

Post by Hernan Joel Cervantes Rodrigu » Wed, 08 Nov 2000 04:00:00



--

Hi all,

I am not new unix programer. But I having an amazing, to me, problem.

I write a program that read a file serching for to two integer, transform
in float and save into a complex vector. The code is lock like :

   indice = 1;
   while ( !(feof(input)) ) {
     if ( read2int(cdata, input) ) {
        perror(__FILE__);
        return -1;
     }
     vector = realloc(vector, indice*sizeof(complex));
     vector[indice-1] = cdata;
     indice++;
   }

   read2int is just a routine that read to integer, test return of "fread"
function and errno for errors, transform the read integer in float. If
not errors was found this routine returns 0 elsewise -1 is returned.

Me test file have a 1024 pairs of integer, when the 1024-esimo integer pair
is read the while loop is even running.

What is the problem with may code?
Can I substitue the "!feof(input)" by
  if ( ((fread(&intbuff, sizeof(int), 2, input)) == 0) && (errno==0) )?

Any suggestions?

Thank in advance

                                        Hernn

                           Hernn J Cervantes Rodrguez
                           Instituto de Fsica da USP

                           homepage : http://fge.if.usp.br/~hernan/

 
 
 

A basic feof question.

Post by John Gord » Wed, 08 Nov 2000 04:00:00



Quote:> I am not new unix programer. But I having an amazing, to me, problem.
> I write a program that read a file serching for to two integer, transform
> in float and save into a complex vector. The code is lock like :
>    indice = 1;
>    while ( !(feof(input)) ) {
>      if ( read2int(cdata, input) ) {
>         perror(__FILE__);
>         return -1;
>      }
>      vector = realloc(vector, indice*sizeof(complex));
>      vector[indice-1] = cdata;
>      indice++;
>    }
>    read2int is just a routine that read to integer, test return of "fread"
> function and errno for errors, transform the read integer in float. If
> not errors was found this routine returns 0 elsewise -1 is returned.
> Me test file have a 1024 pairs of integer, when the 1024-esimo integer pair
> is read the while loop is even running.
> What is the problem with may code?
> Can I substitue the "!feof(input)" by
>   if ( ((fread(&intbuff, sizeof(int), 2, input)) == 0) && (errno==0) )?
> Any suggestions?

looping on feof() is almost always wrong.  feof() will only return true
when you've attempted to read PAST end-of-file.  if you've read all the
data in the file, but no farther, feof() will still return false.

i would suggest this input loop instead:

  while(fread(&intbuff, sizeof(int), 2, input) == 2)
  {
    /* do stuff */
  }

---
"... What with you being his parents and all, I think that you could
be trusted not to shaft him."  -- Robert Chang, rec.games.board



 
 
 

A basic feof question.

Post by Erik Max Franci » Wed, 08 Nov 2000 04:00:00



Quote:> I am not new unix programer. But I having an amazing, to me, problem.

Your question is a basic C one.

Quote:> I write a program that read a file serching for to two integer,
> transform
> in float and save into a complex vector. The code is lock like :

>    indice = 1;
>    while ( !(feof(input)) ) {

        ...

feof only tests for whether an end-of-file error _has already occurred_
on a stream.  It is _not_ used for testing when a file is _at_ the end.

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ There is no fate that cannot be surmounted by scorn.
\__/ Albert Camus
    Official Omega page / http://www.alcyone.com/max/projects/omega/
 The official distribution page for the popular Roguelike, Omega.

 
 
 

A basic feof question.

Post by Chris Tor » Thu, 09 Nov 2000 04:00:00


Quote:>>    while ( !(feof(input)) ) {



Quote:>feof only tests for whether an end-of-file error _has already occurred_
>on a stream.  It is _not_ used for testing when a file is _at_ the end.

Right.  It is worth adding that there is a fundamental reason that
feof() *has* to work this way, on a Unix system.  Consider when
"input" refers to a device or socket.  In this case, the only way
to tell whether there is input is to attempt to read it.  If feof()
had to predict whether EOF would occur during that read, it would
have to *do* that read, of however-many bytes are desired.

In other words, for feof() to "predict" end-of-file, it would have
to *be* the fread() function, and the fgets() function, and every
other input-reading function.  In which case, why not just call
fread(), or fgets(), or whichever input function you wanted?

Once fread() fails to read a full set of bytes, a separate feof()
becomes useful: it tests the reason for the failure.  There are
two possible reasons for that failure, namely, error (a read() call
returned -1) or EOF (a read() returned 0).  If you want to distinguish
between these, you need some way to diagnose the "reason for last
input-operation failure".
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc


 
 
 

1. feof() on SunOS : A question

Dear Netters,

I have a problem detecting eof condition on a file object, for example:

main()
{
  FILE *rule_file;

    if ((rule_file = fopen("rules.dat", "r")) == NULL) {
        perror("rules.dat");
        exit (-1);
    } /* if */
    yyin = rule_file;
    while (!feof(yyin)) {
       yylex();
    } /* while */

After the first pass thru the yylex() routine, feof value becomes
true and while loop terminates. Is there a trick for using feof()
routine?


Thanks in advance...

Ahmet Sekercioglu

2. problem with newgrp

3. Basic basic troff question??!

4. Spontaneous reboots

5. Basic, Basic SCSI Question

6. Problem during Linux install, can't mount A:

7. very basic basic newbie question

8. OS/2 Backup of Linux Partition

9. Newbie basic questions... many questions

10. Basic and Basic->C converter wanted

11. No ROM BASIC Use Toshiba Basic

12. basic, very basic

13. The most basic of basics..