off_t without -D_FILE_OFFSET_BITS=64 on a file > 2

2019-03-30 23:27发布

1- I'm wondering, what would be the problem if I try to read a file greater than 2GB in size without compiling my program with the option -D_FILE_OFFSET_BITS=64 using off_t and using the second function on this page? would it segfault?

2- I'm planning to use this implementation with off64_t and

#define _LARGEFILE64_SOURCE 1
#define _FILE_OFFSET_BITS 64

Would there be any problem?

3条回答
等我变得足够好
2楼-- · 2019-03-31 00:09
  1. stat() will fail, and errno set to EOVERFLOW in that case. Here's what the linux man page says

    EOVERFLOW  stat()) path refers to a file whose size cannot be
    represented in the type off_t.  This can occur when an application
    

    compiled on a 32-bit platform without -D_FILE_OFFSET_BITS=64 calls stat() on a file whose size exceeds (2<<31)-1 bits.

  2. If you compile with -D_FILE_OFFSET_BITS=64 , you don't need to use off64_t though. You can just continue to use off_t , it'll become 64 bit, and all the functions dealing with files and file sizes will become 64 bit aware.
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-03-31 00:10

Never use off64_t explicitly. Always build your programs with 64 bit file offsets on systems where it's necessary to explicitly specify this. Failure to do so is a major bug which your users will end up hating. No idea why it's not default on modern systems...

查看更多
Viruses.
4楼-- · 2019-03-31 00:28

It oughtn't segfault, but the size of the file won't be reported correctly.

查看更多
登录 后发表回答