0

this is the code I use to display the size of the file "myfile_name"

struct stat stbuf;
if (stat("myfile_name",&stbuf)<0) {
    fprintf(stderr, "\nError : %s \nErrno : %s","stat_big file",strerror(errno));
}

printf("ST_SIZE : %ld",stbuf.st_size);

st_size = -1509949440 (bytes) when the size of the file is 2,785,017,856 bytes (2.5)

I have searched for many hours for the solution, I have tried to add this line : #define _FILE_OFFSET_BITS 64 but it didn't work. Errno tells me that there is no error. Appearently, for the stat function, there is no overflow. In another question the answer was to use %ld because of the type of stbuf.st_size (off_t) but it doesn't work.

The code works perfectly with small files.

You're help is very appreciated

MBaram
  • 3
  • 3

2 Answers2

1

The %ld format specifier expects an argument of type (signed) long, but stbuf.st_size has type off_t. There is no format specifier for off_t. Instead you need to cast to a type that can hold any value in the range of off_t and for which you have a format specifier. For example:

printf("%lld\n", (long long)stbuf.st_size);

or even better:

printf("%jd\n", (intmax_t)stbuf.st_size);
R.. GitHub STOP HELPING ICE
  • 195,354
  • 31
  • 331
  • 669
  • Thank you, it works indeed. But `long long size = (long long) stbuf.st_size;` still produces the negative number. Do you know why ? (I think it is because the compiler thinks `stbuf.st_size` is a long int.) – MBaram Mar 07 '21 at 01:31
  • `long long size = (unsigned long long) stbuf.st_size;char t[20];sprintf(t,"%lld",stbuf.st_size);size = atoll(t);` works – MBaram Mar 07 '21 at 01:43
  • What OS are you using? – R.. GitHub STOP HELPING ICE Mar 07 '21 at 02:59
  • Better to ad ref, maybe "GNU C Library Reference Manual", or other to show that `off_t` is a signed type. Casting to a wide type like `intmax_t` and using `"jd"` is a great idea. – chux - Reinstate Monica Mar 07 '21 at 03:08
  • @R..GitHubSTOPHELPINGICE Windows 10 64bits – MBaram Mar 07 '21 at 12:11
  • @MBaram: Under WSL, or Windows-native? I'm not sure what MSVCRT's `stat` function does, but it's probably badly messed up and might be the cause of what you're seeing. – R.. GitHub STOP HELPING ICE Mar 07 '21 at 23:04
  • @R..GitHubSTOPHELPINGICE Windows-native, I am using mingw to compile it. Yes, you may be right. Because the debugger thinks it is a 32bits integer, – MBaram Mar 08 '21 at 12:58
  • @MBaram: I suspect either mingw or MSVCRT just has the size wrong (32-bit) and is writing a truncated result rather than producing `EOVERFLOW` like it should (admittedly the latter behavior would be unhelpful too). The right fix for this is changing it to 64-bit, which everyone else did 2 decades ago (although glibc on Linux still has it wrong by default unless you use `-D_FILE_OFFSET_BITS=64`...) – R.. GitHub STOP HELPING ICE Mar 08 '21 at 14:08
-1

"man 2 stat" gives the call and the 'struct stat' definition.

It also tells you the header files to inspect (unistd.h, sys/types.h, sys/stat.h)

so "off_t st_size" -> you can find the definition of "off_t".

Have a look, then make sure your printf format specifier matches. e.g. you might need '%lu' if 'off_t' is 'unsigned long', which it could easily be.

Fozzie
  • 17
  • 2