473,378 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Reading binary file finding EOF

How can I find the true EOF for a file that contains 0xFF. When I use
fgetc and it encounters 0xFF it thinks it's the end of file but it
really isn't.

Nov 14 '05 #1
11 4450
sp*********@hotmail.com wrote:
How can I find the true EOF for a file that contains 0xFF. When I use
fgetc and it encounters 0xFF it thinks it's the end of file but it
really isn't.


EOF is no character, it's a condition. The confusion probably comes
from some very old DOS versions where ^Z was an end of file marker.
But everything between 0x00 and 0xFF are completely normal and valid
characters that can be returned by fgetc(). That's why the return
value of fgets() is an int, not a char: since all possible chars are
valid return values a value not in this range, defined as EOF (don't
even speculate about it's value, while it's often -1 that's not
necessary and 1725 would also do) gets returned when the end of the
file is detected. So alway store what you get from fgetc() in an int
(and _not_ a char ) and compare that to EOF.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
I changed the variable from char to int and now it works. Thanks Jens.

Nov 14 '05 #3
<Je***********@physik.fu-berlin.de> wrote in message
news:32*************@uni-berlin.de...
sp*********@hotmail.com wrote:
defined as EOF (don't
even speculate about it's value, while it's often -1 that's not
necessary and 1725 would also do)


No it would not. EOF must be negative.

From ISO/IEC 9899:1999 (E) 7.19.1/3 :
--------------------------------------------------------------
EOF

which expands to an integer constant expression, with type int
and a negative value, that is returned by several functions to
indicate end-of-file, that is, no more input from a stream
--------------------------------------------------------------
Anyway, the value 1725 could easily be valid character value
for implementations whose byte size can accomodate it. Also,
if a member of the basic execution character set is stored in
a char object, its value is guaranteed to be positive.
(6.2.5/3).

-Mike

Nov 14 '05 #4
Mike Wahler <mk******@mkwahler.net> wrote:
<Je***********@physik.fu-berlin.de> wrote in message
news:32*************@uni-berlin.de...
sp*********@hotmail.com wrote:
defined as EOF (don't
even speculate about it's value, while it's often -1 that's not
necessary and 1725 would also do)
No it would not. EOF must be negative. From ISO/IEC 9899:1999 (E) 7.19.1/3 :
--------------------------------------------------------------
EOF which expands to an integer constant expression, with type int
and a negative value, that is returned by several functions to
indicate end-of-file, that is, no more input from a stream
-------------------------------------------------------------- Anyway, the value 1725 could easily be valid character value
for implementations whose byte size can accomodate it. Also,
if a member of the basic execution character set is stored in
a char object, its value is guaranteed to be positive.
(6.2.5/3).


I see. Thanks for the correction.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #5
sp*********@hotmail.com writes:
How can I find the true EOF for a file that contains 0xFF. When I use
fgetc and it encounters 0xFF it thinks it's the end of file but it
really isn't.


See the C FAQ, <http://www.eskimo.com/~scs/C-faq/faq.html>, questions
12.1 and 12.38.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
<posted & mailed>

0xFF is never EOF. You simply:

char ch;
FILE *fp;
..
..
..
for (ch = fgetc(fp); !feof(fp); ch = fgetc(fp))
{
/* do something */
}
or

int ch;
FILE *fp;
..
..
..
for (ch = fgetc(fp); ch != EOF; ch = fgetc(fp))
{
/* do something */
}

sp*********@hotmail.com wrote:
How can I find the true EOF for a file that contains 0xFF. When I use
fgetc and it encounters 0xFF it thinks it's the end of file but it
really isn't.


--
Remove '.nospam' from e-mail address to reply by e-mail
Nov 14 '05 #7
On Sun, 12 Dec 2004 22:45:50 -0500, James McIninch
<ja*******************@comcast.net> wrote in comp.lang.c:

Note, top posting is considered rude here, and in most other technical
discussion groups. It makes discussions very hard to follow. Your
new material belongs after, or interspersed with, material you are
quoting. I have reformatted your post properly.
sp*********@hotmail.com wrote:
How can I find the true EOF for a file that contains 0xFF. When I use
fgetc and it encounters 0xFF it thinks it's the end of file but it
really isn't.
<posted & mailed>

0xFF is never EOF. You simply:

char ch;


WRONG!

As Keith correctly pointed out, see this question in the FAQ:

http://www.eskimo.com/~scs/C-faq/q12.1.html
FILE *fp;
.
.
.
for (ch = fgetc(fp); !feof(fp); ch = fgetc(fp))
{
/* do something */
}
The incorrect snippet above is almost certainly what the OP is doing
that is causing the problem he described.
or

int ch;
Yes, this one is correct.
FILE *fp;
.
.
.
for (ch = fgetc(fp); ch != EOF; ch = fgetc(fp))
{
/* do something */
}


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #8
On Sun, 12 Dec 2004 22:39:14 -0600, Jack Klein wrote:
On Sun, 12 Dec 2004 22:45:50 -0500, James McIninch
<ja*******************@comcast.net> wrote in comp.lang.c:

Note, top posting is considered rude here, and in most other technical
discussion groups. It makes discussions very hard to follow. Your
new material belongs after, or interspersed with, material you are
quoting. I have reformatted your post properly.
sp*********@hotmail.com wrote:
> How can I find the true EOF for a file that contains 0xFF. When I use
> fgetc and it encounters 0xFF it thinks it's the end of file but it
> really isn't.
<posted & mailed>

0xFF is never EOF. You simply:

char ch;


WRONG!


Not wrong.
As Keith correctly pointed out, see this question in the FAQ:

http://www.eskimo.com/~scs/C-faq/q12.1.html
FILE *fp;
.
.
.
for (ch = fgetc(fp); !feof(fp); ch = fgetc(fp))
The code here doesn't use the value of ch to test for end of file, so ch
can be char, which appears to be the point the code is trying to make.
The code does contain a bug however because it fails to test ferror(). If
the fgetc() operation failed due to an error the loop would never
terminate, appearing to process multiple (char)EOF valued "characters".
{
/* do something */
}


The incorrect snippet above is almost certainly what the OP is doing
that is causing the problem he described.
or

int ch;


Yes, this one is correct.
FILE *fp;
.
.
.
for (ch = fgetc(fp); ch != EOF; ch = fgetc(fp))
{
/* do something */
}


For completeness there should be a test like

if (ferror(fp)) {
/* Handle error */
}

here after the loop. If a program encounters an input error it probably
shoudn't act as if it completed successfully.

Lawrence
Nov 14 '05 #9
On Mon, 13 Dec 2004 00:31:43 UTC, sp*********@hotmail.com wrote:
How can I find the true EOF for a file that contains 0xFF. When I use
fgetc and it encounters 0xFF it thinks it's the end of file but it
really isn't.

fgetc() returns int, not char. So define the variable that should
receive the value returned by fgec() as int, not char and anything
works as expected.
--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Nov 14 '05 #10
Lawrence Kirby <lk****@netactive.co.uk> writes:
On Sun, 12 Dec 2004 22:39:14 -0600, Jack Klein wrote:
On Sun, 12 Dec 2004 22:45:50 -0500, James McIninch
<ja*******************@comcast.net> wrote in comp.lang.c: [...]
sp*********@hotmail.com wrote:
> How can I find the true EOF for a file that contains 0xFF. When I use
> fgetc and it encounters 0xFF it thinks it's the end of file but it
> really isn't.

<posted & mailed>

0xFF is never EOF. You simply:

char ch;


WRONG!


Not wrong.


Yes, wrong.
As Keith correctly pointed out, see this question in the FAQ:

http://www.eskimo.com/~scs/C-faq/q12.1.html
FILE *fp;
.
.
.
for (ch = fgetc(fp); !feof(fp); ch = fgetc(fp))


The code here doesn't use the value of ch to test for end of file, so ch
can be char, which appears to be the point the code is trying to make.
The code does contain a bug however because it fails to test ferror(). If
the fgetc() operation failed due to an error the loop would never
terminate, appearing to process multiple (char)EOF valued "characters".


fgetc(fp) returns a value of type int; ch is of type char. The
assignment implicitly converts the int result to type char. If char
is signed, and if the value returned by fgetc(fp) is outside the range
CHAR_MIN..CHAR_MAX, the conversion either yields an implementation-defined
value or raises an implementation-defined signal (C99 6.3.1.3p3).
This will happen either of fgetc() returns EOF or if it returns a
value greater than CHAR_MAX (but less than or equal to UCHAR_MAX).

Making ch an unsigned char might alleviate this, but of course the
correct solution is to use an int and check for ch==EOF.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #11
On Mon, 13 Dec 2004 23:53:14 +0000, Keith Thompson wrote:

....
FILE *fp;
.
.
.
for (ch = fgetc(fp); !feof(fp); ch = fgetc(fp))


The code here doesn't use the value of ch to test for end of file, so ch
can be char, which appears to be the point the code is trying to make.
The code does contain a bug however because it fails to test ferror(). If
the fgetc() operation failed due to an error the loop would never
terminate, appearing to process multiple (char)EOF valued "characters".


fgetc(fp) returns a value of type int; ch is of type char. The
assignment implicitly converts the int result to type char. If char
is signed, and if the value returned by fgetc(fp) is outside the range
CHAR_MIN..CHAR_MAX, the conversion either yields an implementation-defined
value or raises an implementation-defined signal (C99 6.3.1.3p3).
This will happen either of fgetc() returns EOF or if it returns a
value greater than CHAR_MAX (but less than or equal to UCHAR_MAX).


Strictly correct but this is one of those cases where existing usage of
for example the form

int ch;
char buffer[SIZE];

while ((ch = fgetc(fp)) != EOF) {
buffer[pos] = ch;

Nov 14 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
7
by: John Smith | last post by:
I want to read data from a file and assign it to a dynamically allocated array. I don't know the number of data in advance. My approach has been to read the file twice, the first time to determine...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
3
by: Ahmad Jalil Qarshi | last post by:
Hi, I have a text file having size about 2 GB. The text file format is like: Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values For example...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.