473,608 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13852
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**********@h p.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_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.
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 misunderstandin g 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

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

Similar topics

8
3003
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 ABCDEFGHIJKLMNOPQ
1
1777
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 it all back out. Not exactly easy on the memory, but I see no other solution. so far: patchme.seek(offset) patchme.write(a2b_hex(edit)) # the data is in hex first patchme.close
3
399
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 data from the server and close the file. I'm sending multiple request for data and I do not want to close the connecting everytime the entire file has been sent from the server and then reconnect. I'm sending binary data, is there anyway to tell...
103
48579
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 says about it. What the heck does the binary flag mean? -- If our hypothesis is about anything and not about some one or more particular things, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in...
12
7822
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
6505
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 delta binary is pretty huge in size. I have 1 byte file and 2 bytes file, the delta should be 1 byte but somehow it turns out to be 249 bytes using binary formatter. I guess serialization has some other things added to the delta file.
4
8687
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 * * @access public * @param mixed $link Complete path to file (/path/to/file)
0
12069
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 likelihood of CRM success from less than 20 percent to 60 percent. WHITEPAPER :
11
3578
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 Read_bin(ByVal ruta As String) Dim cadena As String = "" Dim dato As Array If File.Exists(ruta) = True Then
0
8050
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8472
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
8130
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
8324
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
6805
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
6000
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
3954
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2464
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
1
1574
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.