M
mark
thanks for any help
In said:thanks for any help
In said:Call fread() in a loop and keep track of how many total bytes were read.
thanks for any help
thanks for ur answer but this will b very inefficent, my question is -
what is the builtin filesize function in c
thnx
mark said:thanks for any help
thanks for ur answer but this will b very inefficent, my question is -
what is the builtin filesize function in c
mark said:thanks for ur answer but this will b very inefficent, my question is -
what is the builtin filesize function in c
thnx
Thanks for your answer, but this will be very inefficient. My question is,
what is the builtin file size function in C?
Thanks.
mark said:thanks for any help
James Kuyper said:An extended discussion started the last time someone asked something
like this. A popular contention was that it's pointless to ask how big a
file is, because at best, the answer you'll get is how big it was at
some time in the past; it might be a different size now. That point of
view has some validity, but it ignores two things:
1. You might be explicitly looking for the current value of a time
dependent quantity, such as keeping track of how fast a file is growing.
2. You might have done something to make sure that the file shouldn't
change in size. This is extremely common, in my experience. There's
often only one unprivileged userid currently authorized to change a
given file. If that userid is mine, it's reasonably safe to assume that
if I'm not currently changing the file, it's size won't change.
Le 11/11/11 22:37, mark a écrit :
This function figures out the size of a file, allocates a buffer
and returns the contents of the file.
#include <stdio.h>
#include <stdlib.h>
char *FileToRam(char *fname)
{
FILE *f = fopen(fname,"rb");
long siz;
char *result;
if (f == NULL)
return NULL;
/* Position yourself at the end of the file,
then get the current position. This gives
you the current position in all systems
except in the DS 9000 or if the file is
longer than what a long can hold */
fseek(f,0,SEEK_END);
siz = ftell(f);
fseek(f,0,SEEK_SET);
/* Now allocate a buffer, fill it and
return it.
result = calloc(1,siz+1);
if (result) {
fread(result,1,siz,f);
}
fclose(f);
return result;
}
Please include the question in the body of your post.
"how can i find the size of a binary file"
<There is no reliable way to do this in portable standard C. You can
read through the file, adding up how many bytes you've read, but
that's both slow and not 100% reliable. An implementation is allowed
to treat a binary file as if it had some implementation-defined
number of null bytes append to it (C99 7.19.2p3), though I don't
know of any implementations that actually do that.
You can open the file (in binary mode), then fseek() to the end of
it, then use ftell() to get the current position. That's *usually*
I'm surprised that no one else has cited the FAQ, so far:
19.12: How can I find out the size of a file, prior to reading it in?
A: If the "size of a file" is the number of characters you'll be
able to read from it in C, it is difficult or impossible to
determine this number exactly.
Under Unix, the stat() call will give you an exact answer.
Several other systems supply a Unix-like stat() which will give
an approximate answer. You can fseek() to the end and then use
ftell(), or maybe try fstat(), but these tend to have the same
James Kuyper said:There isn't one. That was considered to be too OS-dependent to justify
standardizing it. For example, on some operating systems, the only thing
that you can quickly determine is how much space has been allocated to
store a file; how much of that space has actually been used can only be
determined by some procedure equivalent to the fread() method given
above. POSIX provides stat(), lstat(), and fstat(); other OSs provide
other methods.
One approach that works on many systems is fseek(file, 0, SEEK_END)
followed by ftell(file). However, make sure to check for an error return
from fseek() - "A binary stream need not meaningfully support fseek
calls with a whence value of SEEK_END." (7.19.9.2p3).
An extended discussion started the last time someone asked something
like this. A popular contention was that it's pointless to ask how big a
file is, because at best, the answer you'll get is how big it was at
some time in the past; it might be a different size now. That point of
view has some validity, but it ignores two things:
1. You might be explicitly looking for the current value of a time
dependent quantity, such as keeping track of how fast a file is growing.
2. You might have done something to make sure that the file shouldn't
change in size. This is extremely common, in my experience. There's
often only one unprivileged userid currently authorized to change a
given file. If that userid is mine, it's reasonably safe to assume that
if I'm not currently changing the file, it's size won't change.
An implementation is allowed to treat a
binary file as if it had some implementation-defined number of null bytes
append to it (C99 7.19.2p3), though I don't know of any implementations
that actually do that.
CP/M records the size of a file in sectors rather than in bytes. Text
files are terminated by a ^Z ('\x1a') character (and this behaviour
was inherited by DOS and then Windows).
jacob navia said:Le 13/11/11 03:47, Nobody a écrit :
This is not true at least for the last 20 years for MSDOS
and windows...
But well, nothing is bad when fighting the "evil empire",
sure, not even lies...
I am in no way tied to Microsoft but it "should" have gotten
through that this is no longer the case for QUITE a long time.
The behavior is still there when typing from the console,
like the Ctrl-D of unix.
o A file size of course could conceivably change by the time it is acted
on.
But a file can also be deleted between calling fopen() and checking the
return value.
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.