Universal way to find end of file

M

Moikel

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
 
G

Gordon Burditt

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).
 
W

Walter Roberson

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.
 
N

newsman654

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.
 
S

santosh

<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.
 
R

Richard Heathfield

(e-mail address removed) said:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top