Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   how can i find the size of a binary file (http://www.velocityreviews.com/forums/t805827-how-can-i-find-the-size-of-a-binary-file.html)

mark 11-11-2011 09:37 PM

how can i find the size of a binary file
 
thanks for any help

John Gordon 11-11-2011 09:48 PM

Re: how can i find the size of a binary file
 
In <j9k4i8$tah$1@speranza.aioe.org> mark <nospam@nospam.com> writes:

> thanks for any help


Call fread() in a loop and keep track of how many total bytes were read.

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"


mark 11-11-2011 09:53 PM

Re: how can i find the size of a binary file
 
> In <j9k4i8$tah$1@speranza.aioe.org> mark <nospam@nospam.com> writes:
>
>> thanks for any help

>
> Call fread() in a loop and keep track of how many total bytes were read.


thanks for ur answer but this will b very inefficent, my question is -
what is the builtin filesize function in c

thnx

jacob navia 11-11-2011 09:59 PM

Re: how can i find the size of a binary file
 
Le 11/11/11 22:37, mark a écrit :
> thanks for any help


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;
}

jacob navia 11-11-2011 10:00 PM

Re: how can i find the size of a binary file
 
Le 11/11/11 22:53, mark a écrit :
>> In<j9k4i8$tah$1@speranza.aioe.org> mark<nospam@nospam.com> writes:
>>
>>> thanks for any help

>>
>> Call fread() in a loop and keep track of how many total bytes were read.

>
> thanks for ur answer but this will b very inefficent, my question is -
> what is the builtin filesize function in c
>
> thnx


See my reply in this same thread.


Keith Thompson 11-11-2011 10:03 PM

Re: how can i find the size of a binary file
 
mark <nospam@nospam.com> writes:
> thanks for any help


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*
going to be the size of the file, but it's still not 100% portable
for the reasons stated above. Furthermore, ftell() returns a long
int; if long int is 32 bits on your system, it's not going to work
for files that are 2 GiB or bigger.

Your operating system probably provides a way to get this information
directly. On Unix-like systems, stat() does this ("man 2 stat"
for details). On other systems, consult your documentation or ask
in a system-specific forum.

This happens to be one of those things that's much easier to do in
a system-specific way than by using portable C.

And watch out for race conditions. Whatever method you use will tell you
the size of the file at the moment when you did the query. The file can
grow, shrink, or even vanish between that and the time when try to do
something with the information.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

James Kuyper 11-11-2011 10:20 PM

Re: how can i find the size of a binary file
 
On 11/11/2011 04:53 PM, mark wrote:
>> In <j9k4i8$tah$1@speranza.aioe.org> mark <nospam@nospam.com> writes:
>>
>>> thanks for any help

>>
>> Call fread() in a loop and keep track of how many total bytes were read.

>
> thanks for ur answer but this will b very inefficent, my question is -
> what is the builtin filesize function in c


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.

Keith Thompson 11-11-2011 10:25 PM

Re: how can i find the size of a binary file
 
mark <nospam@nospam.com> writes:
>> In <j9k4i8$tah$1@speranza.aioe.org> mark <nospam@nospam.com> writes:
>>
>>> thanks for any help

>>
>> Call fread() in a loop and keep track of how many total bytes were read.

>
> thanks for ur answer but this will b very inefficent, my question is -
> what is the builtin filesize function in c
>
> thnx


I think you mean:

> Thanks for your answer, but this will be very inefficient. My question is,
> what is the builtin file size function in C?
>
> Thanks.


If you take the time to spell out words, it will make it easier for the
rest of us to read what you have to say (especially those for whom
English is not a first language) and will generally make us more
inclined to help you.

In answer to your question, there is none; see my other followup for
details.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Ben Pfaff 11-11-2011 10:32 PM

Re: how can i find the size of a binary file
 
mark <nospam@nospam.com> writes:

> thanks for any help


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
sorts of problems: fstat() is not portable, and generally tells
you the same thing stat() tells you; ftell() is not guaranteed
to return a byte count except for binary files. Some systems
provide functions called filesize() or filelength(), but these
are obviously not portable, either.

Are you sure you have to determine the file's size in advance?
Since the most accurate way of determining the size of a file as
a C program will see it is to open the file and read it, perhaps
you can rearrange the code to learn the size as it reads.

References: ISO Sec. 7.9.9.4; H&S Sec. 15.5.1; PCS Sec. 12 p.
213; POSIX Sec. 5.6.2.

--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}

Ike Naar 11-11-2011 10:41 PM

Re: how can i find the size of a binary file
 
On 2011-11-11, mark <nospam@nospam.com> wrote:
> [how can i find the size of a binary file]


This is a FAQ, see http://c-faq.com/osdep/filesize.html


All times are GMT. The time now is 12:45 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.