fseeko64() and ftello64() for deal with large files (eg. file dimension)

AuSsIeStOnE 2 Tallied Votes 481 Views Share

fseek() and ftell() work only for files < 2,147,483,647 bytes

fseeko64() and ftello64() from <stdio.h> can deal with files up to 18,446,744,073,709,552,000 bytes

the printf format for unsigned long long val is %I64d

eg. Obtaining the file dimension (this code was tested with GNU GCC compiler (MinGW/Cygwin) from code::blocks)

#include <stdio.h>
#include <conio.h>

int main ()
{

    FILE * pFile;

    unsigned long long file_dim = 0;
    char file_name[260] = "";

/////////////////////////////////////////////////////////

    printf("ENTER [path] file name.ext:\n");
    gets(file_name);

    pFile = fopen (file_name,"rb");

    if (pFile==NULL)
        perror ("Error opening file");
    else
    {
        fseeko64 (pFile, 0, SEEK_END);
        file_dim = ftello64 (pFile);
        fclose (pFile);
        printf ("File:\n%s\ncontains %I64d bytes:\n", file_name, file_dim);
    }

    printf("Press any key to exit");
    getch();

    return 0;
}