Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 213
Anton Daneyko Avatar asked Dec 15 '12 15:12

Anton Daneyko


3 Answers

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

like image 117
Anton Daneyko Avatar answered Nov 19 '22 05:11

Anton Daneyko


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.

like image 29
Bernd Elkemann Avatar answered Nov 19 '22 07:11

Bernd Elkemann


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.

like image 28
Viswesn Avatar answered Nov 19 '22 05:11

Viswesn