Find eof without using EOF 
Author Message
 Find eof without using EOF

I am reading a data file using:
cTempChar =  fgetc(pFile);

My data file contains bytes such as 0x1A which causes my loop to stop when
checking for EOF.
e.g.
while((cTempChar =  fgetc(pFile)) != EOF)

Is there another way to know when the end of a file is reached?

Thanks
Josh



Sun, 03 Apr 2005 22:38:02 GMT  
 Find eof without using EOF

Quote:

> I am reading a data file using:
> cTempChar =  fgetc(pFile);

> My data file contains bytes such as 0x1A which causes my loop to stop when
> checking for EOF.
> e.g.
> while((cTempChar =  fgetc(pFile)) != EOF)

> Is there another way to know when the end of a file is reached?

EOF is _not_ 0x1A.  For one, it's _negative_.  Often -1.  But
it cannot be 1A hexadecimal (26 decimal) on a system with
a compliant compiler.

It seems that you're talking about some OS-specific way of
specifying the end of file.  You should probably ask in a forum
dedicated to your OS.

Victor
--
Please remove capital A's from my address when replying by mail



Sun, 03 Apr 2005 22:47:54 GMT  
 Find eof without using EOF
Josh> I am reading a data file using:
Josh> cTempChar =  fgetc(pFile);

Josh> My data file contains bytes such as 0x1A which causes my loop
Josh> to stop when checking for EOF.
Josh> e.g.
Josh> while((cTempChar =  fgetc(pFile)) != EOF)

Josh> Is there another way to know when the end of a file is reached?

Yeah -- don't use a char to hold the result of fgetc.

        int cTempChar;

--



Sun, 03 Apr 2005 22:58:24 GMT  
 Find eof without using EOF

Quote:

> I am reading a data file using:
> cTempChar =  fgetc(pFile);

> My data file contains bytes such as 0x1A which causes my loop to stop when
> checking for EOF.
> e.g.
> while((cTempChar =  fgetc(pFile)) != EOF)

> Is there another way to know when the end of a file is reached?

Zje whole topic sounds as if you are using either DOS or Windows
where 0x1A == Ctrl+Z
and Ctrl-Z marks the end of the stream, *in a text stream*

Did you open the file in binary ???

--
Karl Heinz Buchegger



Sun, 03 Apr 2005 23:10:03 GMT  
 Find eof without using EOF
On Thu, 17 Oct 2002 00:38:02 +1000, Josh said:

Quote:
> I am reading a data file using:
> cTempChar =  fgetc(pFile);

> My data file contains bytes such as 0x1A which causes my loop to stop when
> checking for EOF.
> e.g.
> while((cTempChar =  fgetc(pFile)) != EOF)

Try this.

{
  int tmp;

  while((tmp = fgetc(pFile)) != EOF)
  {
    unsigned char uc = (unsigned char) tmp ;
    /* The cast is superfluous, but (IMHO) useful for explanatory
     * purposes */

    /* Do stuff to your character here */
  }

Quote:
}

Cheers,
Dave.

--
           David Neary,
     E-Mail: bolsh at gimp dot org
CV: http://www.redbrick.dcu.ie/~bolsh/CV/CV.html



Sun, 03 Apr 2005 23:11:58 GMT  
 Find eof without using EOF

Quote:

> I am reading a data file using:
> cTempChar =  fgetc(pFile);

> My data file contains bytes such as 0x1A which causes my loop to
> stop when checking for EOF.
> e.g.
> while((cTempChar =  fgetc(pFile)) != EOF)

> Is there another way to know when the end of a file is reached?

Open the file as binary.  The rest is up to the OS.  Which is why
this is OT on this newsgroup and you should find something dealing
with your OS.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Mon, 04 Apr 2005 00:35:33 GMT  
 Find eof without using EOF

Quote:

>while((cTempChar =  fgetc(pFile)) != EOF)

>Is there another way to know when the end of a file is reached?

If you're allowed to open the file as a C++ stream (after all, you *did*
crosspost this to comp.lang.c++):

ifstream pFile ("myfile.dat");

char cTempChar;
while (pFile.get(cTempChar))
{
   // do something with cTempChar

Quote:
}

--

Dept. of Physics and Computer Science        Clinton, South Carolina USA


Mon, 04 Apr 2005 01:38:33 GMT  
 Find eof without using EOF

Quote:

> I am reading a data file using:
> cTempChar =  fgetc(pFile);

> My data file contains bytes such as 0x1A which causes my loop to stop when
> checking for EOF.
> e.g.
> while((cTempChar =  fgetc(pFile)) != EOF)

> Is there another way to know when the end of a file is reached?

    This is Question 12.38 in the comp.lang.c Frequently
Asked Questions (FAQ) list

        http://www.eskimo.com/~scs/C-faq/top.html

--



Mon, 04 Apr 2005 01:49:35 GMT  
 Find eof without using EOF

Quote:

> I am reading a data file using:
> cTempChar =  fgetc(pFile);

> My data file contains bytes such as 0x1A which causes my loop to stop when
> checking for EOF.
> e.g.
> while((cTempChar =  fgetc(pFile)) != EOF)

> Is there another way to know when the end of a file is reached?

Your program stops at the character 0x1A, because on some extremely
idiotic operating systems, that character marks the end of a text
file, even though more data can follow, and the underlying file system
is perfectly capable of indicating where the true data ends. That is
the convention, and you are stuck with it.

The C language streams provide a platform-independent abstraction of
text file in the form of streams that can be open in a text mode. In
text mode, ANSI C streams perform whatever translation is necessary on
input and output to make the stream look sane to the programmer. Lines
appear to be terminated with single newline characters ('\n'), and
there doesn't appear to be any special character marking the end of
the file.

You have two problems here. Firstly, the result of fgets() is type
int, not type char. The reason it is of type int is to that it can
return a character value, or the special value EOF which *is not a
character*, but an abstract indication which says that no more
characters are available, or an I/O error occured! If you try to
capture this value using a char type, can run into problems due to the
narrowing conversion yielding false indicators---legitimate character
values that turn into EOF, or the opposite problem: the EOF value
turning into something that is not equal to EOF.

Secondly, you are probably trying to read a binary file, but opened
the stream in text mode! So the library is interpreting the first
random occurence of the byte 0x1b as the end of what it thinks is a
text file; and what's worse, you are getting line ending conversion.

Use the "b" option in the fopen() mode!



Mon, 04 Apr 2005 02:39:59 GMT  
 Find eof without using EOF
On Wed, 16 Oct 2002 14:38:02 UTC, "Josh"

Quote:

> I am reading a data file using:
> cTempChar =  fgetc(pFile);

> My data file contains bytes such as 0x1A which causes my loop to stop when
> checking for EOF.
> e.g.
> while((cTempChar =  fgetc(pFile)) != EOF)

> Is there another way to know when the end of a file is reached?

> Thanks
> Josh

fgetc() returns int, not char. Expecting EOF in a char would always
fail.
--
Tschau/Bye

Herbert Rosenau
http://www.pc-rosenau.de   eComStation Reseller in Germany



Mon, 04 Apr 2005 07:25:52 GMT  
 Find eof without using EOF

Quote:


> > I am reading a data file using:
> > cTempChar =  fgetc(pFile);

> > My data file contains bytes such as 0x1A which causes my loop to
> > stop when checking for EOF.
> > e.g.
> > while((cTempChar =  fgetc(pFile)) != EOF)

> > Is there another way to know when the end of a file is reached?

> Open the file as binary.  The rest is up to the OS.  Which is why
> this is OT on this newsgroup and you should find something dealing
> with your OS.

Am I the only one who knows about feof(),
or is that the wrong answer ?

Last draft C89:
The fgetc function returns the next character from the input stream
pointed to by stream .  If the stream is at end-of-file, the
end-of-file indicator for the stream is set and fgetc returns EOF .
If a read error occurs, the error indicator for the stream is set and
fgetc returns EOF .
   The feof function returns nonzero if and only if the end-of-file
indicator is set for stream .

--
 pete



Mon, 04 Apr 2005 09:12:05 GMT  
 Find eof without using EOF
On Wed, 16 Oct 2002 21:12:05 -0400,

Quote:


>> > I am reading a data file using:
>> > cTempChar =  fgetc(pFile);

>> > My data file contains bytes such as 0x1A which causes my loop to
>> > stop when checking for EOF.
>> > e.g.
>> > while((cTempChar =  fgetc(pFile)) != EOF)

>> > Is there another way to know when the end of a file is reached?

>> Open the file as binary.  The rest is up to the OS.  Which is why
>> this is OT on this newsgroup and you should find something dealing
>> with your OS.

> Am I the only one who knows about feof(),
> or is that the wrong answer ?

In this case it most likely is. It looks like the OP opened the file in
text mode, and on the OP's OS 0x1A in a text file most likely indicates
end-of-file. Opening the file in binary mode will probably fix this, but
that is a question for an os-specific group.

feof() will allow you to distinguish between an end-of-file condition
and another condition that would make fgetc() return EOF, like an input
error, for example (see also ferror()). Most likely on the OP's OS the
presence of 0x1A in the file is a valid end-of-file condition.

Martien
--
                        |
Martien Verbruggen      | My friend has a baby. I'm writing down all
                        | the noises the baby makes so later I can ask
                        | him what he meant - Steven Wright



Mon, 04 Apr 2005 10:30:46 GMT  
 Find eof without using EOF

Quote:
>I am reading a data file using:
>cTempChar =  fgetc(pFile);

>My data file contains bytes such as 0x1A which causes my loop to stop when
>checking for EOF.
>e.g.
>while((cTempChar =  fgetc(pFile)) != EOF)

>Is there another way to know when the end of a file is reached?

You're confusing the logical end of a text file and the physical end of
a file.  

If you open the file in binary mode, characters like 0x1A lose their
special meaning and fgetc() will keep reading until the physical end
of file is reached.

BTW, EOF is a negative value, so be sure to declare cTempChar as
int, otherwise you may be unable to discern between EOF and a legit
character value (short of using the feof() function).

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Mon, 04 Apr 2005 18:05:18 GMT  
 Find eof without using EOF

Quote:
>On Wed, 16 Oct 2002 14:38:02 UTC, "Josh"

>> I am reading a data file using:
>> cTempChar =  fgetc(pFile);

>> My data file contains bytes such as 0x1A which causes my loop to stop when
>> checking for EOF.
>> e.g.
>> while((cTempChar =  fgetc(pFile)) != EOF)

>> Is there another way to know when the end of a file is reached?

>> Thanks
>> Josh

>fgetc() returns int, not char. Expecting EOF in a char would always
>fail.                                                        ^^^^^^

Sometimes, but not *always*.  This explains why people keep making this
mistake without being bitten from the first attempt.

Consider a typical example: char is a signed type and the file only
contains ASCII characters.  Using char instead of int will not affect
the expected behaviour of the code.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Mon, 04 Apr 2005 18:48:54 GMT  
 Find eof without using EOF

Quote:

>> I am reading a data file using:
>> cTempChar =  fgetc(pFile);

>> My data file contains bytes such as 0x1A which causes my loop to stop when
>> checking for EOF.
>> e.g.
>> while((cTempChar =  fgetc(pFile)) != EOF)

>> Is there another way to know when the end of a file is reached?

>Your program stops at the character 0x1A, because on some extremely
>idiotic operating systems, that character marks the end of a text
>file, even though more data can follow, and the underlying file system
>is perfectly capable of indicating where the true data ends. That is
>the convention, and you are stuck with it.

Note that this convention originated on some simple, but not at all
idiotic operating systems, which needed a *simple* method of marking the
logical end of a text file, because the physical end of *any* file was
at the end of disk block (the filesystem didn't store the size of a file
in bytes anywhere).

Later, the convention was adopted by other operating systems, for
compatibility purposes: it allowed the more sophisticated OS (which
didn't need the convention at all for its native text files) to import
text files from the older OSs and still be able to properly detect the
logical end of the file.

There is no idiocy involved here (except that of the programmer who
opens a binary file in text mode).

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Mon, 04 Apr 2005 18:42:50 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. When Is EOF not EOF?

2. EOF or not EOF

3. Cannot find EOF

4. Finding EOF

5. Proper use of EOF - or another way to find

6. CFile finds EOF too early

7. trying to find a eof marker

8. to avoid going out of EOF using fseek

9. Detecting EOF

10. Unexpected behavior reading EOF in midstream

11. STDIN and EOF

12. EOF problem -- HELP!!

 

 
Powered by phpBB® Forum Software