473,406 Members | 2,439 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,406 software developers and data experts.

why can this code tell the EOF of binary file?

Hi,
I know that text file ended with EOF mark but there is no mark for
binary file.
So, the problem is how do we know the end of binary file is reach?

This code can tell us when the end of file is reach

int ch;

FILE *fp;

fp = fopen("filename","rb");

if(fp != NULL) {
while((ch = getc(fp)) != EOF) {
/* your code */
}

}

My question is , since there is no ending mark up for binary file, why
does the code above work?
also, why does built-in function feof(fp) know where is end-of-file?
thank you for all discussions.
chat watchara

Jan 4 '07 #1
10 13830
chat wrote:
I know that text file ended with EOF mark but there is no mark for
binary file.
This is not true.

How a text /file/ ends isn't defined by the C standard. What /is/
defined is what happens when you try and read the postultimate
character of a FILE* [connected to a file] using (f)getc: you get
the magic value EOF. This is true whether or not you're reading
a "text file".

[However, this EOF may be preceeded by some number of 0's -- ISTR
it's zeroes -- not logically present in the file, because some
systems at some time did or still do not record the exact number
of bytes kept in a file: files may contain integral numbers of
/blocks/ of bytes.]
So, the problem is how do we know the end of binary file is reach?
Wait for EOF.
also, why does built-in function feof(fp) know where is end-of-file?
Because it's its business to know [that the last read failed because
it was at end-of-file]. Note that `feof` is often the wrong function
to be calling.

--
Chris "especially for eg Fourier transforms" Dollin
"We did not have time to find out everything we wanted to know."
- James Blish, /A Clash of Cymbals/

Jan 4 '07 #2
chat writes:
I know that text file ended with EOF mark but there is no mark for
binary file.
So, the problem is how do we know the end of binary file is reach?
A file in a file system normally has an associated file size which gives
the file's physical size in the file system. That may be a size in
bytes, in which case no "EOF mark" is needed to mark the end of text
files. But if the size is in blocks, then a binary file has a size
which is a multiple of the block size. (So does a text file as far as
the associatead file size is concerned, but an "EOF mark" can delimit it
before the end of physical file.)
(...)
fp = fopen("filename","rb");
if(fp != NULL) {
while((ch = getc(fp)) != EOF) {
/* your code */
}

}

My question is , since there is no ending mark up for binary file, why
does the code above work?
It ends when it reaches the physical end of the file.
also, why does built-in function feof(fp) know where is end-of-file?
feof() returns true when getc() has returned EOF, not before.

--
Hallvard
Jan 4 '07 #3
"chat" <ch***********@gmail.comwrote:
I know that text file ended with EOF mark but there is no mark for
binary file.
So, the problem is how do we know the end of binary file is reach?
We don't have to; the implementation has to tell us (by returning EOF
instead of a value byte, e.g.), and it typically gets that information
from the underlying operating system.
My question is , since there is no ending mark up for binary file,
Well, there is. It just isn't inside the binary file data itself.
Usually it's a length field in the directory structure, or a block count
in the disk allocation tables, or something external like that.
also, why does built-in function feof(fp) know where is end-of-file?
It doesn't know that itself. All feof() does is signal that the last
function used to read from a stream signalled end of file; and those,
again, get that information from the OS, which gets it from the
directory structure or something similar.

Richard
Jan 4 '07 #4
chat said:
Hi,
I know that text file ended with EOF mark but there is no mark for
binary file.
On some systems that is true, but the EOF mark you're talking about bears no
relation to C's EOF indicator, which is *not* a character stored on the
file system, but a special return value provided by getc (and one or two
other functions) to mean "sorry pal, nothing left to read". This works just
as well on binary files as on text files, and it works just as well on file
systems that do *not* store a byte meaning "end of file" at the end of text
files or indeed binary files.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 4 '07 #5
Chris Dollin <ch**********@hp.comwrites:
[...]
How a text /file/ ends isn't defined by the C standard. What /is/
defined is what happens when you try and read the postultimate
character of a FILE* [connected to a file] using (f)getc: you get
the magic value EOF. This is true whether or not you're reading
a "text file".

[However, this EOF may be preceeded by some number of 0's -- ISTR
it's zeroes -- not logically present in the file, because some
systems at some time did or still do not record the exact number
of bytes kept in a file: files may contain integral numbers of
/blocks/ of bytes.]
Only binary files may be padded with zero bytes. With a text file,
you'll read only the characters that were actually written to it (with
some possible translation for end-of-line markers et al).

--
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.
Jan 4 '07 #6

chat wrote:
Hi,
I know that text file ended with EOF mark but there is no mark for
binary file.
So, the problem is how do we know the end of binary file is reach?

This code can tell us when the end of file is reach

int ch;

FILE *fp;

fp = fopen("filename","rb");

if(fp != NULL) {
while((ch = getc(fp)) != EOF) {
/* your code */
}

}

My question is , since there is no ending mark up for binary file, why
does the code above work?
also, why does built-in function feof(fp) know where is end-of-file?
thank you for all discussions.
chat watchara
EOF is a condition. It is not something that exists in a file.

--
aegis

Jan 5 '07 #7
Thank you all discussions. Binary file is complicated than I think. The
way we can know where is the end-of-file is to ask the OS. The OS must
responsible for this job.
thank you very much.
chat watchara

Jan 5 '07 #8
chat wrote:
Thank you all discussions. Binary file is complicated than I think.
Not if you understand it's limitations.
The way we can know where is the end-of-file is to ask the OS.
Actually, in a standard C program, failure to read from a stream, (and
hence from a disk file), is reported by returning the value represented
by the macro EOF for some functions, (getc, fgetc, getchar, scanf
etc.), by returning the value NULL for some functions, (fgets, gets),
and by returning a value other than what is expected, (fread).

To know whether the failure was caused by the stream being at
end-of-file or by an I/O error, you'll have to use the functions feof()
and ferror() immediatly after the failure is reported. Note that feof()
and ferror() work only with the standard library's I/O functions. If
you call other non-standard functions like POSIX read() or an OS API,
then you cannot use feof() or ferror().

So, in summary, for binary files, though you can know the end of input
in standard C, you cannot know the actual bytes making up the file
without resorting to non-standard functions.

Jan 5 '07 #9

chat wrote:
Thank you all discussions. Binary file is complicated than I think.
I think you must be misunderstanding something. Binary files are simple
and straightforward.
The way we can know where is the end-of-file is to ask the OS.
That's one way to do it. In pure C the way to do it is read characters
until EOF is returned.
The OS must responsible for this job.
The OS is clearly responsible for knowing how big the file is. The C
library works with the OS to make sure that you will get EOF after you
read the last character of the file.

Jan 5 '07 #10
Keith Thompson wrote:
Chris Dollin <ch**********@hp.comwrites:
[...]
>How a text /file/ ends isn't defined by the C standard. What /is/
defined is what happens when you try and read the postultimate
character of a FILE* [connected to a file] using (f)getc: you get
the magic value EOF. This is true whether or not you're reading
a "text file".

[However, this EOF may be preceeded by some number of 0's -- ISTR
it's zeroes -- not logically present in the file, because some
systems at some time did or still do not record the exact number
of bytes kept in a file: files may contain integral numbers of
/blocks/ of bytes.]

Only binary files may be padded with zero bytes.
I /said/ that -- wait, I didn't, I only thought I did. Oops.

Thanks for spotting that, Keith.

--
Chris "hopefully not Pyecroft" Dollin
"A facility for quotation covers the absence of original thought." /Gaudy Night/

Jan 5 '07 #11

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

Similar topics

8
by: Peter Abel | last post by:
Hi all, I'm working under W2k with Python 2.2.2 (#37, Oct 14 2002, 17:02:34) on win32 I have a file *test_data.txt* with the following content: 0123456789 0123456789 abcdefghi...
1
by: Matt | last post by:
I'd like to overwrite just one line of a binary file, based on a position set by seek(). Is there no way to do this? As far as I can tell I need to read the whole file, change the line, and write...
3
by: DJTN | last post by:
I have created 2 vb.net applications in VS 2002, a server and a client using the .net.sockets namespace. I can connect and receive data fine but the client cannot tell when it has recived all the...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
12
by: Sunner Sun | last post by:
Hi, all Since the OS look both ASCII and binary file as a sequence of bytes, is there any way to determine the file type except to judge the extension? Thank you!
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
4
by: comp.lang.php | last post by:
I borrowed the following function from the PHP manual user notes: if (!function_exists('is_binary')) { /** * Determine if a file is binary. Useful for doing file content editing * *...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...
0
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...

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.