James wrote:

> i'm trying to use stat() to find out a file's size. So far i have :
>
> /* Start Of Code */
> #include <sys/stat.h>
> #include <unistd.h>
> #include <stdio.h>
>
> int main (void)
> {
>         int status;
>         struct stat *buf;
>
>         status = stat ("/root/.procmailrc", buf);
>         /* don't ask why i'm using my procmailrc, it's the first file */
>         /* i thought of */
>
>         return 0;
> }
> /* End Of Code */
>
> that should have filled buf with info about that file, but how do i go about
> displaying the filesize? i know it's in buf->st_size, but that's of type
> off_t (what's that?) and printf() segfaults when i try to do
>
>         printf ("File is %d bytes\n", buf->st_size);
>
> well actually i get a warning when compiling.
>
> --
> [EMAIL PROTECTED]                http://x-map.home.ml.org


You'll need to allocate memory for *buf. You can do this by adding:

  buf = (struct stat *)malloc(sizeof(struct stat));
  if (! buf)  {
     fprintf(stderr, "Memory allocation failure\n");
     exit(EXIT_FAILURE);
  }

also be sure to free the allocated memory before ending via "free(buf);".

---------- But, there is an easier way without using malloc...

simply do this:

 struct stat  buff;
 const char *f_n = "/root/.procmailrc";

 if ( stat ( f_n, &buf) != 0)
    exit(EXIT_FAILURE);
 else
    printf("sizeof %s is %ld\n", f_n, buf.st_size)0\;

 exit(EXIT_SUCCESS);


That should do it okay... Keep in mind when using the functions  stat(),
lstat(), fstat(), that you do not need access rights to the file(s) being
examined but, you do need to have search rights to ALL the directories
specified in the path leading to the file....

See the attatched file for a more concise example...

/John < [EMAIL PROTECTED]>





--
email: [EMAIL PROTECTED]
Local mailserver  , remote

Abolish capital punishment throughout the world!


#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>


int main()
{

   struct stat f_inf;
   const char *f_n = "/home/holtko/.signature";


   if ( stat(f_n, &f_inf) != 0) {
     exit(EXIT_FAILURE);
   }
   else {
     fprintf(stdout, "size_of %s = %ld\n", f_n, f_inf.st_size);
   }


 exit(EXIT_SUCCESS);

}
  

Reply via email to