简体   繁体   English

从二进制文件读取数据

[英]Reading data from binary file

I am trying to read data from binary file, and having issues. 我正在尝试从二进制文件读取数据,并且出现问题。 I have reduced it down to the most simple case here, and it still won't work. 我已经将其简化为最简单的情况,但仍然无法使用。 I am new to c++ so I may be doing something silly but, if anyone could advise I would be very grateful. 我是c ++的新手,所以我可能会做一些愚蠢的事情,但是,如果有人可以建议,我将非常感激。

Code: 码:

int main(int argc,char *argv[]) {
    ifstream myfile;
    vector<bool> encoded2;

    cout << encoded2 << "\n"<< "\n" ;

    myfile.open(argv[2], ios::in | ios::binary |ios::ate );
    myfile.seekg(0,ios::beg);
    myfile.read((char*)&encoded2, 1 );
    myfile.close();


    cout << encoded2  << "\n"<< "\n" ;

}

Output 输出量

00000000 00000000

000000000000000000000000000011110000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000100011110000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Compression_Program(58221) malloc: * error for object 0x10012d: Non-aligned pointer being freed * set a breakpoint in malloc_error_break to debug Compression_Program(58221)malloc: *对象0x10012d错误:释放了未对齐的指针*在malloc_error_break中设置一个断点以进行调试

Thanks in advance. 提前致谢。

Do not cast a vector<bool>* to a char* . 不要将vector<bool>*转换为char* It is does not do anything predictable. 它没有做任何可预测的事情。

Two mistakes here: 这里有两个错误:

  • you assume the address of a vector is the address of the first element 您假设向量的地址是第一个元素的地址
  • you rely on vector<bool> 你依靠vector<bool>

Casting a vector into a char * is not really a good thing, because a vector is an object and stores some state along with its elements. 将向量强制转换为char *并不是一件好事,因为向量是一个对象,并存储一些状态及其元素。

Here you are probably overwriting the state of the vector, thus the destructor of fails. 在这里,您可能覆盖了向量的状态,因此失败的析构函数。

Maybe you would like to cast the elements of the vector (which are guaranteed to be stored contiguously in memory). 也许您想转换向量的元素(保证将它们连续存储在内存中)。 But another trap is that vector<bool> may be implementation-optimized. 但是另一个陷阱是vector<bool>可能是实现优化的。

Therefore you should do a encoded2.reserve(8) and use myfile.read(reinterpret_cast<char *>(&encoded2[0])) . 因此,您应该执行encoded2.reserve(8)并使用myfile.read(reinterpret_cast<char *>(&encoded2[0]))

But probably you want to do something else and we need to know what the purpose is here. 但是可能您想做其他事情,我们需要知道这里的目的是什么。

You are reading on encoded2: myfile.read((char*)&encoded2, 1 ); 您正在阅读encoding2: myfile.read((char*)&encoded2, 1 ); . this is wrong. 这是错误的。 you can to read a bool and then put it in encoded2 您可以阅读bool,然后将其放入encoding2

bool x;
myfile.read( &x, 1 );
encoded2[0] = x;

You're overwriting a std::vector , which you shouldn't do. 您正在覆盖std::vector ,而不应该这样做。 A std::vector is actually a pointer to a data array and an integer (probably a size_t ) holding its size; 一个std::vector实际上是一个指向数据数组的指针,以及一个保存其大小的整数(可能是size_t )。 if you overwrite these with practically random bits, data corruption will occur. 如果使用几乎随机的位覆盖这些位,则会发生数据损坏。

Since you're only reading a single byte, this will suffice: 由于您只读取一个字节,因此就足够了:

char c;
myfile.read(&c, 1);

The C++ language does not provide an efficient I/O method for reading bits as bits. C ++语言没有提供一种有效的I / O方法来将位读取为位。 You have to read bits in groups. 您必须成组读取位。 Also, you have to worry about Endianess when reading int the bits. 另外,在读取int位时,您还必须担心Endianess。

I suggest the old fashioned method of allocating a buffer, reading into the buffer then operating on the buffer. 我建议使用一种老式的方法来分配缓冲区,读入缓冲区然后在缓冲区上进行操作。

Allocating a buffer 分配缓冲区

const unsigned int BUFFER_SIZE = 1024 * 1024; // Let the compiler calculate it.
//...
unsigned char * const buffer = new unsigned char [BUFFER_SIZE];  // The pointer is constant.

Reading in the data 读取数据

unsigned int bytes_read = 0;
ifstream data_file("myfile.bin", ios::binary); // Open file for input without translations.
data_file.read(buffer, BUFFER_SIZE); // Read data into the buffer.
bytes_read = data_file.gcount();  // Get actual count of bytes read.

Reminders: 温馨提示:

  • delete the buffer when you are finished with it. delete缓冲区。
  • Close the file when you are finished with it. 完成后关闭文件。
myfile.read((char*) &encoded2[0], sizeof(int)* COUNT);

or you can use push_back(); 或者您可以使用push_back();

int tmp;
for(int i = 0; i < COUNT; i++) {
  myfile.read((char*) &tmp, 4);
  encoded2.push_back(tmp);
}

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

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