简体   繁体   English

从二进制文件读取时获取错误的数据

[英]Getting wrong data back when reading from binary file

I'm having an issue reading in some bytes from a yuv file (it's 1280x720 if that matters) and was hoping someone could point out what I'm doing wrong. 我从yuv文件中读取某些字节时遇到问题(如果重要的话,它的分辨率为1280x720),希望有人指出我做错了什么。 I'm getting different results using the read command and using an istream iterator . 我使用read命令和istream迭代器得到了不同的结果。 Here's some example code of what I'm trying to do: 这是我正在尝试执行的一些示例代码:

void readBlock(std::ifstream& yuvFile, YUVBlock& destBlock, YUVConfig& config, const unsigned int x, const unsigned int y, const bool useAligned = false)
{
    //Calculate luma offset
    unsigned int YOffset = (useAligned ? config.m_alignedYFileOffset : config.m_YFileOffset) +
    (destBlock.yY * (useAligned ? config.m_alignedYUVWidth : config.m_YUVWidth) + destBlock.yX);// *config.m_bitDepth;

//Copy Luma data
//yuvFile.seekg(YOffset, std::istream::beg);
    for (unsigned int lumaY = 0; lumaY < destBlock.m_YHeight && ((lumaY + destBlock.yY) < config.m_YUVHeight); ++lumaY)
    {
        yuvFile.seekg(YOffset + ((useAligned ? config.m_alignedYUVWidth : config.m_YUVWidth)/* * config.m_bitDepth*/) * (lumaY), std::istream::beg);
        int copySize = destBlock.m_YWidth;
        if (destBlock.yX + copySize > config.m_YUVWidth)
        {
            copySize = config.m_YUVWidth - destBlock.yX;
        }
        if (destBlock.yX >= 1088 && destBlock.yY >= 704)
        {
            char* test = new char[9];
            yuvFile.read(test, 9);

            delete[] test;
            yuvFile.seekg(YOffset + ((useAligned ? config.m_alignedYUVWidth : config.m_YUVWidth)/* * config.m_bitDepth*/) * (lumaY));
        }

        std::istream_iterator<uint8_t> start = std::istream_iterator<uint8_t>(yuvFile);
        std::copy_n(start, copySize, std::back_inserter(destBlock.m_yData));

    }
}
struct YUVBlock
{
std::vector<uint8_t> m_yData;
std::vector<uint8_t> m_uData;
std::vector<uint8_t> m_vData;
unsigned int m_YWidth;
unsigned int m_YHeight;
unsigned int m_UWidth;
unsigned int m_UHeight;
unsigned int m_VWidth;
unsigned int m_VHeight;

unsigned int yX;
unsigned int yY;
unsigned int uX;
unsigned int uY;
unsigned int vX;
unsigned int vY;
};

This error only seems to be happening at X =1088 and Y = 704 in the image. 该错误似乎仅在图像中的X = 1088和Y = 704处发生。 I'm expecting to see a byte value of 10 as the first byte I read back. 我希望看到的字节值10是我读回的第一个字节。 When I use 当我使用

yuvFile.read(test, 9);

I get 10 as my first byte. 我的第一个字节为10。 When I use the istream iterator: 当我使用istream迭代器时:

std::istream_iterator<uint8_t> start = std::istream_iterator<uint8_t>(yuvFile);
    std::copy_n(start, copySize, std::back_inserter(destBlock.m_yData));

The first byte I read is 17. 17 is the byte after 10 so it seems the istream iterator skips the first byte. 我读取的第一个字节是17。17是10之后的字节,因此istream迭代器似乎跳过了第一个字节。

Any help would be appreciated 任何帮助,将不胜感激

There is a major difference between istream::read and std::istream_iterator . istream::readstd::istream_iterator之间存在主要区别。

std::istream::read performs unformatted read. std::istream::read执行未格式化的读取。
std::istream_iterator performs formatted read. std::istream_iterator执行格式化读取。

From http://en.cppreference.com/w/cpp/iterator/istream_iterator 来自http://en.cppreference.com/w/cpp/iterator/istream_iterator

std::istream_iterator is a single-pass input iterator that reads successive objects of type T from the std::basic_istream object for which it was constructed, by calling the appropriate operator>> . std::istream_iterator是单次输入迭代器,它通过调用适当的operator>>std::basic_istream构造了std::basic_istream对象中读取类型T连续对象。

If your file was created using std::ostream::write or fwrite , you must use std::istream::read or fread to read the data. 如果文件是使用std::ostream::writefwrite ,则必须使用std::istream::readfread读取数据。

If your file was created using any of the methods that create formatted output, such as std::ostream::operato<<() , fprintf , you have a chance to read the data using std::istream_iterator . 如果您的文件是使用任何创建格式化输出的方法(例如std::ostream::operato<<()fprintf ,那么您就有机会使用std::istream_iterator读取数据。

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

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