0

write a program to find size of a file without traversing it character by character?

will anybody tell me the logic hw to approach it

is there any trick involved

7
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by Ancient Dragon
0

Every file ends with an EOF character.Find out what the functions which ArkM has stated does and use the above information and you are done.

0

are you using *nix?

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

int getFileSize(char *filename, long *filesize)
{
   struct stat filestats;              

   if (stat(filename, &filestats) < 0) {
   { 
      perror(filename);
      return 0; 
   } 
   else
      *filesize = filestats.st_size;

   printf(" The size of %s is %ld bytes (%3.1f KB)\n", filename, *filesize, (*filesize / 1024.0) );
   return 1;
}

.

0

Those C functions may not work with huge files -- files that are larger than 2 gig. In MS-Windows win32 api functions would be needed to get the file size of huge files. I don't know about *nix or MAC

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.