C : Get Size of Big File

I want to get size of file in C (this program can handling open big input file >4GB). I use function fseek and ftell to get size of file. But the problem when we use this command is fseek and ftell can not handling open big file input (>4GB). Because return value from command ftell is an integer data type. If we want to get size of file (C program can handling open big file input) we can use function fseeko64 and ftello64. This function exist in header sys/types.h (I use Ubuntu Linux).

I will give an example how to get size of big file (C handling big file input) with useĀ  use function fseeko64 and ftello64. This program use input from stdin and compute size of file from that input file. Save this code as cbigfile.c :

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

int main(int argc, char **argv)
{
  FILE *fid;
  int retseek;
  off64_t endPos;  /*size of file input*/
  off64_t bytetr;  /*size of trace data and data */

  retseek = fseeko64(stdin, 0 , SEEK_END); /* go to end of file */
  if(retseek){
    printf("error seek command.");
    exit(0);
  }

  endPos = ftello64(stdin);                /* get size of file */
  printf("size file from stdin = %lld byte\n", endPos);

  return(1);
}

Compile this C get size of big file code with command :

gcc cbigfile.c -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -o cbigfile

This is a sample output when this C program open and get size of big file (12GB).

./cbigfile < /data/Download/FILM/iso.iso
size file from stdin = 12905986806 byte

Thank for reading my post about how to open and get size of big file in C.

Source :
http://www.fedoraforum.org/forum/showthread.php?t=187982

Add a Comment

Your email address will not be published. Required fields are marked *