Anton Daneyko
Anton Daneyko

Reputation: 6512

Calling stat from <sys/stat.h> faills with "Value too large for defined data type" error

Given tmp.c:

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

int main(int argc, const char *argv[])
{
    struct stat st;
    if (stat(argv[1], &st) != 0)
    {
        perror("Error calling stat");
    }

    return 0;
}

I get Error calling stat: Value too large for defined data type, when I run the program on a large file (~2.5 Gb).

Upvotes: 8

Views: 10368

Answers (4)

Anton Daneyko
Anton Daneyko

Reputation: 6512

One needs to #define _FILE_OFFSET_BITS 64: either add it before you #include <sys/stat.h> or define it in your platform-specific way e.g., for gcc see -D option; for Visual Studio go to project properties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions

Upvotes: 10

Bernd Elkemann
Bernd Elkemann

Reputation: 23560

If others have this problem and the _FILE_OFFSET_BITS 64 before #include "sys/stat.h" did not solve it yet, just move it in front of all other include's too. I did not find out which headers also depended on this but it solved the problem.

Upvotes: 3

ravi bhuva
ravi bhuva

Reputation: 342

you are able to remove this limitation by including header file config.h in your program. and this is not necessary to include whole file but you are also able to put one macro #define _FILE_OFFSET_BITS 64 to remove limitation.

Upvotes: 0

Viswesn
Viswesn

Reputation: 4880

Have a look in to this link . It provides you the way to handle such issue.

This is typically done by defining -D_FILE_OFFSET_BITS=64 or some such. It is system dependent. Once done and once switched into this new mode most programs will support large files just fine.

Upvotes: 1

Related Questions