简体   繁体   English

读取二进制数据,seek 无法正常工作

[英]Read binary data, seek not working correctly

I have been having some issues with trying to read data out of a binary file that is structured so that the header is the first 1024, or 4069 bytes, and the payloads are all regular blocks after that.我在尝试从结构化的二进制文件中读取数据时遇到了一些问题,因此 header 是前 1024 或 4069 字节,之后的有效负载都是常规块。 Below is a model of the code that I have been using:下面是我一直在使用的代码的 model:

Header: Header:

#pragma once
#include <stdio.h>
#include <iostream> 
#include <fstream>

class BinaryFile
{
public:
    long MagicNumber;
    long HeaderSize;
    long PayloadSize;
    //... about 20 other header details
    char* Padding; // unused area of the header that is reserved for future use
    std::ifstream BinaryFileStream;

    BinaryFile();
    int Open_Binary_File(const char* path);
    int Load_Payload_Into_Buffer(int payload_index, void* buffer);
};

And the C++ code that I have tried to use:还有我尝试使用的 C++ 代码:

#include "BinaryFile.h"

BinaryFile:BinaryFile()
{
    MagicNumber = 0;
    HeaderSize = 1024;
    PayloadSize = 62830080;
    // ... all the other header items, initalized with default values
    std:ifstream BinaryFileStream;
    Padding[360];
}

int BinaryFile::Open_Binary_File(const char* path) // This is the one that I would like to be using
{
    BinaryFileStream.open(path, std::ifstream::binary);
    BinaryFileStream.read((char*)&MagicNumber, (size_t) 4);
    BinaryFileStream.read((char*)&HeaderSize, (size_t) 4);
    // ... The rest of the header is placed into the object
    BinaryFileStream.read((char*)&Padding, (size_t) 360); 
    // The file pointer should now be 1024 bytes into the file, at the end of the header, and at the start of the first payload
    return 0;
}

int Load_Payload_Into_Buffer(int payload_index, void* buffer)
{
    buffer = malloc(PayloadSize);
    size_t offset = HeaderSize + static_cast<long long>(frame_index) * PayloadSize;
    BinaryFileStream.seekg(offset, BinaryFileStream.beg);
    BinaryFileStream.read((char*)buffer, PayloadSize);
    return 0;
Error:
    return 1;
}

Below are some variations that I have tried:以下是我尝试过的一些变体:

int BinaryFile::Open_Binary_File(const char* path)
{
    BinaryFileStream.open(path, std::ifstream::binary);
    BinaryFileStream.read((char*)&MagicNumber, (size_t) 4);
    BinaryFileStream.read((char*)&HeaderSize, (size_t) 4);
    // ... The rest of the header is placed into the object
    BinaryFileStream.read((char*)&Padding, (size_t) 360);
    BinaryFileStream.clear();
    BinaryFileStream._Seekbeg.seekg((size_t)0, BinaryFileStream._Seekbeg);
    BinaryFileStream.sync();
    // The file pointer should now be 0 bytes into the file, at the start of the header
    return 0;
}

int BinaryFile::Open_Binary_File(const char* path)
{
    BinaryFileStream.open(path, std::ifstream::binary);
    BinaryFileStream.read((char*)&MagicNumber, (size_t) 4);
    BinaryFileStream.read((char*)&HeaderSize, (size_t) 4);
    // ... The rest of the header is placed into the object
    BinaryFileStream.read((char*)&Padding, (size_t) 360);
    BinaryFileStream.clear();
    BinaryFileStream.seekg((size_t)-1024);
    BinaryFileStream.sync();
    // The file pointer should now be 0 bytes into the file, at the start of the header
    return 0;
}

The issue that I am running into to is, the payload that is being returned to the buffer contains some of the next payload.我遇到的问题是,返回到缓冲区的有效负载包含一些下一个有效负载。 The setting of the file pointer doesn't seem to be working the way that I expect it to.文件指针的设置似乎不像我期望的那样工作。 ie if I say即如果我说

BinaryFileStream._Seekbeg.seekg(position, BinaryFileStream._Seekbeg)

I expect the pointer to return to the start of the file, and then seek along the number of bytes that I have said in position.我希望指针返回到文件的开头,然后沿着我在 position 中所说的字节数查找。 Is there a different way to do this?有不同的方法可以做到这一点吗? or is there something that I am missing?还是有什么我想念的?

Turns out, what I needed to do was multiply the HeaderSize by CHAR_BIT so it would look something like this:事实证明,我需要做的是将HeaderSize乘以CHAR_BIT ,所以它看起来像这样:

int Load_Payload_Into_Buffer(int payload_index, void* buffer)
{
    buffer = malloc(PayloadSize);
    size_t offset = HeaderSize*CHAR_BIT + static_cast<long long>(frame_index) * PayloadSize;
    BinaryFileStream.seekg(offset, BinaryFileStream.beg);
    BinaryFileStream.read((char*)buffer, PayloadSize);
    return 0;
Error:
    return 1;
}

And what I thought was a portion of the next payload, was in fact the same payload, but split at some arbitrary point, and stacked next to it.我认为是下一个有效载荷的一部分,实际上是相同的有效载荷,但在某个任意点拆分,并堆叠在它旁边。 So the payload was split approx.所以有效载荷被分割了大约。 3/4 of the way through, and it was then re-arranged, so that the original last quarter was now at in the first position, and the original first 3/4 was now in the 2nd position. 3/4,然后重新排列,原来的最后一个季度现在在第一个 position,原来的前 3/4 现在在第二个 position。

Hopefully that makes some sense, and will help someone in the future!希望这是有道理的,并会在未来帮助某人!

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

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