How to get a file's size which is greater than 4 Gb?

I made a simple function, which get's a file's size.

int file_size( char * filename )
     int size;
     struct stat st;

     stat( filename, &st );
     size = st.st_size;

     return size;


It is working fine, but if i have a file which size is bigger than 4Gb than i get a negativ number back, and of course this isn't the correct file size. So how can i get such a big file's size? I think, that the return value should anything else like int but i don't know what, and i don't think, that would solve my problem.





I found the solution. I have to use __stat64. I modifyed my function, and now it is retrieving the real size. I tested it with an 8Gb big file.

unsigned long long int file_size( char * filename )
    unsigned long long int size;
    struct __stat64 st;

    __stat64( filename, &st );
    size = st.st_size;

   return size;


And a notice:

When i used printf, i had to use "%I64d" to print it out.


You can probably use _stat64.

You can use the Win32 GetFileSizeEx function.

HANDLE aFile = CreateFile

  long long aSize;
  GetFileSizeEx(aFile, &aSize);

  return aSize;
  return -1;

In such cases, return value of stat() might be -1, and errno set to EOVERFLOW.

The man page for stat on my (64 bit x86 machine) says:


(stat()) path refers to a file whose size cannot be represented in the type off_t. This can occur when an application compiled on a 32-bit platform without -D_FILE_OFFSET_BITS=64 calls stat() on a file whose size exceeds (2<<31)-1 bits.

st_size is defined as long in wchar.h, so you could probably try with long, instead of int

struct stat {
    _off_t     st_size;


typedef long _off_t;  

May be like this

long file_size( char * filename )
     long size;
     struct stat st;

     stat( filename, &st );
     size = st.st_size;

     return size;


I think , problem is because of file size more than INT_MAX , So that it is giving you negative values .

Probably you can do type cast for long data type , it may work.

Otherwise use , fseek , ftell function . then subtract end file pointer from start file pointer . It will also give you file size. ( number of bytes ) .

Need Your Help

System services not available to activities before onCreate() on trying to implement getSystemService()

java android

So I'm trying to implement a AlertDialog which should open up on pressing the the 'activate' button, but on pressing the button, the app crashes.

Need to Toggle DIV Visibility for 2 DIVs


I need show a DIV full of dimensions and weights in English measure, and have a link allowing the user to toggle to a hidden DIV that instead displays Metric. Trying the markup below, but it's not

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.