473,786 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4508
sp*********@hot mail.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***********@p hysik.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*********@hot mail.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******@mkwah ler.net> wrote:
<Je***********@ physik.fu-berlin.de> wrote in message
news:32******** *****@uni-berlin.de...
sp*********@hot mail.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***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #5
sp*********@hot mail.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_Keit h) 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*********@hot mail.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*********@hot mail.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.l earn.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*********@hot mail.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*********@hot mail.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

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

Similar topics

13
15263
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 pattern can occur on any byte boundary in the file, so chunking through the code at 16 bytes a frame maybe a problem. The file itself isn't so large, maybe 32 kbytes is all and the need for speed is not so great, but the need for accuracy in the...
20
3078
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: --cut here-- typedef struct pkg_ { short int info; char* data;
6
3796
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 trying to read a flatfile. In COBOL, my simple file layout and READ statement would look like below. Question: what is the standard, simple coding convention for reading in a flatfile - one record at a time?? SCANF does not work because of...
7
6063
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 populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
30
4588
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
2184
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 its size, the second for the actual assignment. Is there a more efficient way?
6
5274
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
9885
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 should loop 15,870,080 times. The code is: newprogram.cpp =============
3
2838
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 (!file.eof ()) { do {
3
2165
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 consider following chunk of actual data:
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.