stat.st_size
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > stat.st_size




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    stat.st_size  
Roopa


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-10-05 12:51 PM

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






[ Post a follow-up to this message ]



    Re: stat.st_size  
Villy Kruse


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-10-05 10: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





[ Post a follow-up to this message ]



    Re: stat.st_size  
Rich Teer


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-10-05 10: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





[ Post a follow-up to this message ]



    Re: stat.st_size  
Geoff Clare


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-11-05 11: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>






[ Post a follow-up to this message ]



    Re: stat.st_size  
Ravi


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-14-05 07:51 AM

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>






[ Post a follow-up to this message ]



    Re: stat.st_size  
Geoff Clare


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-14-05 10: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>






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 05:48 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register