• 0

c++ checking file size!


Question

for all interested in what i am actually doing, i am currently making a DB editor for autopatcher! but i have come into a snag, the db has another file, called a .lst file, which is just a list of every refference to a file in the db in the format of:

filename

filesize

filename

filesize

etc

and i have been searching the net to no avail on a way to quickly and easily check the file size of a file and give it to me in bytes

so how do i check the size of a file?

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

/* ftell example : getting size of a file */
#include <stdio.h>

int main ()
{
  FILE * pFile;
  long size;

  pFile = fopen ("myfile.txt","rb");
  if (pFile==NULL) perror ("Error opening file");
  else
  {
    fseek (pFile, 0, SEEK_END);
    size=ftell (pFile);
    fclose (pFile);
    printf ("Size of myfile.txt: %ld bytes.\n",size);
  }
  return 0;
}

From: http://www.cplusplus.com/ref/cstdio/ftell.html

Link to comment
Share on other sites

  • 0

Here is a way of doing it without opening the file:

/* STAT.C: This program uses the _stat function to
 * report information about the file named STAT.C.
 */

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

void main( void )
{
   struct _stat buf;
   int result;
   char buffer[] = "A line to output";

   /* 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 ) );
   }
}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.