|
Home > Archive > Unix Programming > February 2005 > stat.st_size
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| Hi,
If my "file" contain 10 textual characters in it does this
program gives me number of bytes in the file i.e 10 ?
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
int main( void )
{
struct stat buf;
if( stat( "file", &buf ) != -1 ) {
printf( "File size = %d\n", buf.st_size );
}
return EXIT_SUCCESS;
}
If not how does one go about finding all the bytes in a file
efficiently.
TIA
Roopa
| |
| Villy Kruse 2005-02-10, 5:58 pm |
| On 10 Feb 2005 03:30:58 -0800,
Roopa <roopa.ravi@gmail.com> wrote:
> Hi,
>
> If my "file" contain 10 textual characters in it does this
> program gives me number of bytes in the file i.e 10 ?
>
With the conventional meaning of characters yes. Obviously, for
multibyte characters like utf-8 or wide characters like unicode or
CJK that would not be the case.
Villy
| |
| Rich Teer 2005-02-10, 5:58 pm |
| On Thu, 10 Feb 2005, Roopa wrote:
> If my "file" contain 10 textual characters in it does this
> program gives me number of bytes in the file i.e 10 ?
What's preventing you from compiling it and seeing the result?
--
Rich Teer, SCNA, SCSA, author of "Solaris Systems Programming"
President,
Rite Online Inc.
Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich
| |
| Geoff Clare 2005-02-11, 6:03 pm |
| "Roopa" <roopa.ravi@gmail.com> wrote, on Thu, 10 Feb 2005:
> If my "file" contain 10 textual characters in it does this
> program gives me number of bytes in the file i.e 10 ?
>
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/stat.h>
>
> int main( void )
> {
> struct stat buf;
>
> if( stat( "file", &buf ) != -1 ) {
> printf( "File size = %d\n", buf.st_size );
> }
> return EXIT_SUCCESS;
> }
This program will do one of three things, depending on what
system it is used on and how it is compiled:
1. It may print the number of bytes in any file.
2. It may print the number of bytes in most files (such
as your 10-byte one) but print nothing for very large
files because stat()fails.
3. It may not work at all (it might print garbage or maybe
even core dump).
Typical for case 1 is a compilation environment where off_t is 32-bit
on a system that can not create files larger than 2GB.
Typical for case 2 is a compilation environment where off_t is 32-bit
on a system that can create files larger than 2GB.
Typical for case 3 is a compilation environment where off_t is 64-bit
and int is 32-bit. This case is "undefined behaviour" (meaning the
system can do absolutely anything it likes) because buf.st_size has
type off_t but you have told printf() to expect an int.
--
Geoff Clare <netnews@gclare.org.uk>
| |
|
| Geoff Clare wrote:
> "Roopa" <roopa.ravi@gmail.com> wrote, on Thu, 10 Feb 2005:
>
>
> This program will do one of three things, depending on what
> system it is used on and how it is compiled:
>
> 1. It may print the number of bytes in any file.
>
> 2. It may print the number of bytes in most files (such
> as your 10-byte one) but print nothing for very large
> files because stat()fails.
>
> 3. It may not work at all (it might print garbage or maybe
> even core dump).
>
> Typical for case 1 is a compilation environment where off_t is 32-bit
> on a system that can not create files larger than 2GB.
How do you find that (2 GBLimit) ?
On my system i find 'off_t' is 32 bit and int is 32 bit
>
> Typical for case 2 is a compilation environment where off_t is 32-bit
> on a system that can create files larger than 2GB.
>
Same here
> Typical for case 3 is a compilation environment where off_t is 64-bit
> and int is 32-bit. This case is "undefined behaviour" (meaning the
> system can do absolutely anything it likes) because buf.st_size has
> type off_t but you have told printf() to expect an int.
>
> --
> Geoff Clare <netnews@gclare.org.uk>
| |
| Geoff Clare 2005-02-14, 5:54 pm |
| "Ravi" <raviuday@gmail.com> wrote, on Sun, 13 Feb 2005:
>
> How do you find that (2 GBLimit) ?
If you mean how can a person find out, the best way is to read the
documentation for the relevant file system type (ufs, vxfs, ext2, etc.).
If you mean how can a program find out, the standard way is with
pathconf(path, _PC_FILESIZEBITS) or "getconf FILESIZEBITS", but not
all systems support those, and some that do support them give
bogus results. E.g. on the Linux system I'm typing this on:
$ getconf FILESIZEBITS /proc
32
is correct (the proc filesystem would never have files larger then 2GB),
but:
$ getconf FILESIZEBITS /
64
is wrong because / is an ext3 file system which has a maximum file
size much less than 2^63-1 bytes. At least it does indicate that
files larger than 2GB are supported.
> On my system i find 'off_t' is 32 bit and int is 32 bit
You may find you can compile programs with a 64-bit off_t by
adding -D_FILE_OFFSET_BITS=64 to the compiler options. A lot of
32-bit systems support this method of getting a 64-bit off_t.
--
Geoff Clare <netnews@gclare.org.uk>
|
|
|
|
|