c++ - stat64 在 64 位 ubuntu 上返回 32 位 st_size

标签 c++ linux

unsigned char *map_file(char *filename, uint64_t *len) {
uint64_t fd = open64(filename, O_RDONLY);

struct stat64 st;
fstat64(fd, &st)

unsigned char *map;
map = (unsigned char *)mmap64(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);

大文件的 st.st_size 最终为 4294967295(我正在针对 8.7gb 文件进行测试)并导致段错误(47%)。机器是 64 位的,操作系统 (ubuntu) 是 64 位的。我做错了什么?

最佳答案

您可能需要定义其中一个宏。

http://www.kernel.org/doc/man-pages/online/pages/man7/feature_test_macros.7.html

  _LARGEFILE64_SOURCE
          Expose definitions for the alternative API specified by the LFS (Large
          File Summit) as a "transitional extension" to the Single UNIX
          Specification.  (See http://opengroup.org/platform/lfs.html.)  The
          alternative API consists of a set of new objects (i.e., functions and
          types) whose names are suffixed with "64" (e.g., off64_t versus off_t,
          lseek64() versus lseek(), etc.).  New programs should not employ this
          interface; instead _FILE_OFFSET_BITS=64 should be employed.

  _FILE_OFFSET_BITS
          Defining this macro with the value 64 automatically converts references
          to 32-bit functions and data types related to file I/O and file system
          operations into references to their 64-bit counterparts.  This is
          useful for performing I/O on large files (> 2 Gigabytes) on 32-bit
          systems.  (Defining this macro permits correctly written programs to
          use large files with only a recompilation being required.)  64-bit
          systems naturally permit file sizes greater than 2 Gigabytes, and on
          those systems this macro has no effect.

关于c++ - stat64 在 64 位 ubuntu 上返回 32 位 st_size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5072128/

相关文章:

c++ - 为什么在这种情况下使用 RemovAll() 时 CArray 会崩溃?

c++ - 我是否应该为 C++ 中的每种类型都有一个解析器?

python - 尝试使用 Cython 扩展时 undefined symbol

linux - 如何在同一个linux系统上拥有不同的lua版本

c++ - 基于字符串对结构数组进行排序

c++ - 如何使用宏根据参数定义不同的数据类型?

c++ - 调用VxGUI Constructor L6218E错误

c++ - GLEW 1.10.0段错误

Linux组权限?

linux - 更新派生文件以包含原始文件的更改