Click to See Complete Forum and Search --> : File size w/o opening file


pschiff
July 21st, 2003, 01:01 PM
Can anyone remind me how to in C, get the size of a disk file WITHOUT having to open it. thank you.

HeartBreakKid
July 21st, 2003, 01:06 PM
#include <sys/stat.h>

struct stat s;

_stat("MyFile.TXT", &s);

int size = s.st_size

Philip Nicoletti
July 21st, 2003, 01:08 PM
It's not standard C, but most compilers support _stat() ...

From MSDN


#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>

void main( void )
{
struct _stat buf;
int result;

/* Get data associated with "stat.c": */
result = _stat( "stat.c", &buf );

/* Check if statistics are valid: */
if( result != 0 )
perror( "Problem getting information" );
else
{
/* Output some of the statistics: */
printf( "File size : %ld\n", buf.st_size );
printf( "Drive : %c:\n", buf.st_dev + 'A' );
printf( "Time modified : %s", ctime( &buf.st_atime ) );
}
}