Hi Vijay,

By looking with google and experimenting, I found out that your posix
version will work if you cast the parameter in the structure properly:

printf("Size of %s, uid %d, is %d\n", argv[1], sb.st_uid, (long)sb.st_size);

The "(long)sb.st_size" being the key thing.

As discussed here: http://curl.haxx.se/mail/lib-2003-12/0232.html

http://curl.haxx.se/mail/lib-2003-12/0237.html

So the type of st_size is off_t

Regards,

     Mox

On 12/28/06, Vijay Venkatraman <[EMAIL PROTECTED]> wrote:

Hi,
   I am writing a simple program to print the size of a file on disk. But
surprisingly my program keeps printing 0 on Darwin (but works on a PC
running Linux). Here's two set of programs I wrote, one using stat posix api
and other using Carbon CoreServices. Neither of them works. If someone can
tell me whats wrong or suggest a way of getting file size correctly, please
let me know.

Thanks
Vijay

Posix:
--------
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

main(int argc, char **argv)
{
  struct stat sb;
  int retval;

  if(argc<2){
        fprintf(stderr,"Usage: %s <filename>\n", argv[0]);
        exit(1);
  }

  retval = stat(argv[1], &sb);

  if(retval==0) printf("Size of %s, uid %d, is %d\n", argv[1], sb.st_uid,
sb.st_size);
  else perror("stat");
}

CoreServices:
-------------------
OSStatus PrintFileSize(char *path)
{
   OSStatus err=noErr;
   FSRef ref;
   FSCatalogInfo cataloginfo;

   err = FSPathMakeRef("/Users/vijayv/dd.txt", &ref, NULL);

   require_noerr(err, CantMakeRef);

   err = FSGetCatalogInfo (&ref, kFSCatInfoDataSizes, &cataloginfo, NULL,
NULL, NULL);

   require_noerr (err, CantGetCatalogInfo);

   printf("File size is %d\n", cataloginfo.dataLogicalSize);

    return err;

CantMakeRef:
    printf("Cant make ref\n");

CantGetCatalogInfo:
    printf("Cant get catalog info\n");

    return err;
}



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com




--
Mox on G

Reply via email to