c++ - 在 Ubuntu 64 位操作系统中无法读取大于 2GB 的文件

标签 c++ linux ubuntu limit

已解决 @nsilent22,感谢您的建议。我所做的唯一更改是在eekg()函数参数中。

ND_file.seekg( (long)(block_number)*DISK_BLOCK_SIZE );
ND_file.read((char*)(&count), sizeof(int));

原帖

我知道之前已经有人提出过类似的问题并得到了回答。仍需要现有程序的帮助。

硬件规范:Ubuntu 14.04,64 位,ext4 文件系统

我有一个用于创建基于磁盘的索引的旧程序。当索引文件达到2.1GB时,下面的read()函数输入错误的值,程序中止。

ND_file.seekg(block_number*DISK_BLOCK_SIZE);
ND_file.read((char*)(&count), sizeof(int));

基于类似的帖子,我已将 #define _FILE_OFFSET_BITS 64 放在每个文件的开头。

以下是生成文件:

GCC=g++ -D_FILE_OFFSET_BITS=64
NDT: main.o utility.o logClass.o
     g++ -g -o ndTree main.o utility.o logClass.o
     rm *.o
main.o: main.cpp config.h Box_queries.h Meta_entry.h Meta_node.h Dir_entry.h Dir_node.h Leaf_entry.h Leaf_node.h logClass.h ND_tree.h Node.h utility.h
     g++ -g -c main.cpp
utility.o: utility.cpp utility.h
     g++ -g -c utility.cpp
logClass.o: logClass.cpp logClass.h
     g++ -g -c logClass.cpp
clean:
    rm -f *.o

@已更新

输出日志的一部分:这里Block-174被调用了两次。在第一种情况下,读取函数正确输入磁盘数据。但是,在最后一行中,#Entries 为 0,这是不正确的。当分配 block::524288 时就会发生这种情况。因此,我猜想read()和seekg()不能正常工作。

Id       NewBlockId   ParentBlockId  #EntriesInBlock  Index #MaxEntries   
554192   524280       463255         149              61    255       
554193   524281       463255         149              75    255
554194   524282       497389         131              80    255
**554195     524283       174            250              26    255**
554196   524284       125426         142              77    255
554197   524285       223509         130             118    255
554198   524286       262212         136              72    255
554199   524287       224407         142             121    255
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check
**554200     524288       174              0             227    255** Aborted (core dumped)

我应该使用不同的函数来读取文件内容吗?任何建议将不胜感激。

最佳答案

答案很简单。根据您的评论:

const int DISK_BLOCK_SIZE; unsigned int block_number;

两个变量的类型都是int(一个无符号),因此计算出的值为...int。 而 int 在 64 位 Ubuntu 系统上的大小是……32 位。在您的 64 位系统上编译这个简单的代码:

#include <stdio.h>

int main(void) {
    int x = 2000000000;
    int y = 4;
    printf("x = %d\n", x);
    printf("y = %d\n", y);
    printf("x * y = %d\n", x * y);
    printf("x * y = %ld\n", (long)(x * y));
    printf("x * y = %ld\n", (long)(x) * y);
    return 0;
}

然后你就会明白你应该做什么。

关于c++ - 在 Ubuntu 64 位操作系统中无法读取大于 2GB 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31596965/

相关文章:

ubuntu - 通过 npm 安装 karma 在 phantomjs 上超时

来自多个类的 C++ 多态性

C++ 程序输出错误的数字

c++ - long long int 数组指针 该数组的算法

linux - 带有连续管道的 grep 不起作用

linux - BeagleBone Black 不检测 USB

ruby-on-rails - 如何让 systemd 使用 Puma 重启 Rails App

c++ - 断言在具有单元测试的 C++ 程序中的作用是什么?

python - 我的家没有 .pypirc 文件,这在将 python 包注册到 PyPI 时给我一个错误

json - 使用 jq 解析不带分隔符的 JSON 字符串