CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2002
    Posts
    137

    File size w/o opening file

    Can anyone remind me how to in C, get the size of a disk file WITHOUT having to open it. thank you.

  2. #2
    Join Date
    Jul 2002
    Location
    St. Louis, MO
    Posts
    484
    Code:
    #include <sys/stat.h>
    
    struct stat s;
    
    _stat("MyFile.TXT", &s);
    
    int size = s.st_size

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    It's not standard C, but most compilers support _stat() ...

    From MSDN

    Code:
    #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 ) );
       }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured