简体   繁体   English

读取二进制文件

[英]Reading of binary file

I got segmentation problem while reading binary file.我在读取二进制文件时遇到了分段问题。

File size is 16859136 bytes, containing double values, but in fact I can read about 4214784 bytes (4 times less).文件大小为 16859136 字节,包含双精度值,但实际上我可以读取大约 4214784 字节(少 4 倍)。

I use the following c++ code.我使用以下 C++ 代码。

const int size = 6*28*28*28*4*4; // 16859136(bytes) = size * 8(bytes)  
double data[size];

ifstream in;
in.open("/path/to/file", std::fstream::binary);
if (in.is_open()) { cout << "File is open!\n"; }

in.read((char *) &data, sizeof(data));
cout << in.gcount() << " bytes read\n";

in.close();

Output:输出:

Segmentation fault分段故障

PS: Maybe there is some kind of syntax error... PS:也许有某种语法错误......

The following lines allocate a space too large on your stack:以下几行在您的堆栈上分配了一个太大的空间:

const int size = 6*28*28*28*4*4; // 16859136(bytes) = size * 8(bytes)  
double data[size];

consider instead a heap allocation:考虑堆分配:

std::vector<double> data(6*28*28*28*4*4);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM