By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,436 Members | 1,344 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,436 IT Pros & Developers. It's quick & easy.

Universal way to find end of file

P: n/a
Hi,

I've decided to write a little archiving program (something similar to
a simple winZip) and I need a way to determine the number of bytes in a
file. I'm using getc() to read chars from the file and I have a little
loop that looks something like this:

while(p = getc(file_pointer) != eof)
{
num_bytes++;
}

Now, this works fine for text files, but I tried to use this on a .wmv
video, and got a completely crazy answer. I'm guessing that the end of
these files are not designated by an eof character....

Now, what do I have to do to determine in the number of bytes in a file
of any file type?

Thanks

Dec 28 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
>I've decided to write a little archiving program (something similar to
>a simple winZip) and I need a way to determine the number of bytes in a
file. I'm using getc() to read chars from the file and I have a little
loop that looks something like this:

while(p = getc(file_pointer) != eof)
{
num_bytes++;
}
What's eof? If you meant EOF, that's *NOT* a character, and it's
not guaranteed to fit in one. If you meant eof, that's not defined
by Standard C.
>Now, this works fine for text files, but I tried to use this on a .wmv
video, and got a completely crazy answer.
Completely crazy too short, or completely crazy too long, or completely
crazy overflows the type of num_bytes, which you didn't show above?
>I'm guessing that the end of
these files are not designated by an eof character....
There is no such thing as an "eof character".
If it's a binary file, open it in binary mode.
>Now, what do I have to do to determine in the number of bytes in a file
of any file type?
num_bytes should probably be of type size_t or unsigned long long
(if available).

Dec 28 '06 #2

P: n/a
In article <11**********************@h40g2000cwb.googlegroups .com>,
Moikel <ob*************@gmail.comwrote:
>I've decided to write a little archiving program (something similar to
a simple winZip) and I need a way to determine the number of bytes in a
file.
There is no portable way to do that.

- If you open a file as text, then the system might translate
line-ending pairs into single characters in the buffer

- If you open a file as binary, then the system is (if I recall
correctly) allowed to lose nulls at the end of the file

- Some systems only record the size of a text files in full blocks,
and rely upon a system-specific end-of-file character. On such systems
if you open what "should be" a binary file as text and fgetc()
through it, then the system might detect a valid binary character
as if it were the end of file; if, though, on such systems, you
were to open what "should be" a text file as binary then you
might not be told about the end of file until the end of the block
instead of at the system-magic end-of-file character.
For these and similar reasons, if you want to know the number
of bytes in a file, you must use some system extension (if available.)
It is common for that system extension to be named stat() or fstat()
or similar names, but none of those are defined by the C standard itself.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Dec 28 '06 #3

P: n/a
Moikel <ob*************@gmail.comwrote:

<snip>
a simple winZip) and I need a way to determine the number of bytes in a
file. I'm using getc() to read chars from the file and I have a little
loop that looks something like this:

while(p = getc(file_pointer) != eof)
{
num_bytes++;
}
<snip>

I suggest using the functions ftell and fseek...

I haven't tried it recently but I believe this will do what you want.

First you fseek to the end of the file, then you use the ftell function
to return the number of bytes from the beginning of the file.

I believe this is universal, but I may be wrong.

Dec 28 '06 #4

P: n/a
ne********@sbcglobal.net wrote:
Moikel <ob*************@gmail.comwrote:

<snip>
a simple winZip) and I need a way to determine the number of bytes in a
file. I'm using getc() to read chars from the file and I have a little
loop that looks something like this:

while(p = getc(file_pointer) != eof)
{
num_bytes++;
}

<snip>

I suggest using the functions ftell and fseek...

I haven't tried it recently but I believe this will do what you want.

First you fseek to the end of the file, then you use the ftell function
to return the number of bytes from the beginning of the file.

I believe this is universal, but I may be wrong.
No, the value returned needn't correspond to the actual number of bytes
in the file.

Dec 28 '06 #5

P: n/a
ne********@sbcglobal.net said:

<snip>
>
First you fseek to the end of the file, then you use the ftell function
to return the number of bytes from the beginning of the file.

I believe this is universal, but I may be wrong.
You may not be surprised, then, to hear that you are wrong. The Standard has
this to say about fseek on binary streams: "A binary stream need not
meaningfully support fseek calls with a whence value of SEEK_END." ... and
this on text streams: "For a text stream, either offset shall be zero, or
offset shall be a value returned by an earlier call to the ftell function
on the same stream and whence shall be SEEK_SET."

So, either way, you're stuffed.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 28 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.