How do you determine the size of a file in C?

前端 未结 14 1294
野性不改
野性不改 2020-11-22 03:20

How can I figure out the size of a file, in bytes?

#include 

unsigned int fsize(char* file){
  //what goes here?
}
14条回答
  •  生来不讨喜
    2020-11-22 03:53

    Don't use int. Files over 2 gigabytes in size are common as dirt these days

    Don't use unsigned int. Files over 4 gigabytes in size are common as some slightly-less-common dirt

    IIRC the standard library defines off_t as an unsigned 64 bit integer, which is what everyone should be using. We can redefine that to be 128 bits in a few years when we start having 16 exabyte files hanging around.

    If you're on windows, you should use GetFileSizeEx - it actually uses a signed 64 bit integer, so they'll start hitting problems with 8 exabyte files. Foolish Microsoft! :-)

提交回复
热议问题