不一致的二进制数据或条件语句更改数据 [英] Inconsistent Binary Data or Condition Statement Changing Data

查看:144
本文介绍了不一致的二进制数据或条件语句更改数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真的有连措辞的问题有些麻烦。发生了什么事是我在读和存储一个完整的二进制文件到一个uint32_t的*。然后检查数据的段我需要的开始。然而,增加一个检查,只要确保我没有在阵的变化,其中段的开始是过去了,我不知道为什么。

Really having some trouble even phrasing the problem. What's happening is I'm reading and storing an entire binary file into a uint32_t*. Then checking for the start of the segment of data I need. However, adding a check to just make sure I haven't passed over the array changes where the start of the segment is and I have no idea why.

#include <iostream>
#include <string.h>
#include <typeinfo>
#include <fstream>
#include <vector>
#include <stdint.h>
#include <stdexcept>
using namespace std;

int* binmem = new int[32]; 
streampos size; // size of memblock 
int blocklength; // length memblock
uint32_t * memblock; // all data
int startpos; 
int endpos;  
int x;

void NewParticle() {
    startpos = 0;
    while (memblock[startpos]!= 0xff000000) { // 4278190080
        if (startpos > blocklength) {
            //cout << "nah";
            //x++;
            throw invalid_argument("No start of particle");
        }
        startpos++;
    }
}
int main(int argc, const char *argv[]) {
    ifstream file(argv[0], ios::in | ios::binary | ios::ate);
    if (file.is_open()) {
        size = file.tellg();
        blocklength = (size / 4) + (size % 4 == 0 ? 0 : 1);
        memblock = new uint32_t[size / 4 + (size % 4 == 0 ? 0 : 1)];
        file.seekg(0, ios::beg);
        file.read(reinterpret_cast<char*>(memblock), size);
        file.close();
        NewParticle();
    } else
        cout << "Unable to open file";
    return 0;
}

startpos则取决于什么条件语句我加入到NewParticle(),例如不同而不同。抛出异常给出1109作为在那里完全取出来的条件给812使用全息,使while循环运行,直到永永远使用X ++语句导致段错误...任何想法如何,这些也可能会被改变的东西呢?谢谢

startpos then varies depending on what condition statement I add into NewParticle(), eg. throwing the exception gives 1109 where as taking the condition out completely gives 812. Using the "nah" makes the while loop run forever and using the x++ statement causes a segment fault... Any idea how these could possibly be changing things? Thank you

推荐答案

您code似乎工作,如果你的输入是一个有效的文件。

You code seems to work if your input is a valid file.

由于@RetiredNinja指出,可能出现的问题是:

As @RetiredNinja pointed out, possible problems are:


  1. 的argv [0] 而不是的argv [1]

  1. Using argv [0] instead of argv [1].

的argv [0] 通常指向的可执行文件,这可能不是你想要分析的文件的名称。如果你试图解析当前运行的可执行文件来找到你的粒子,那么你有一个不好的设计。因此,让这种变化:

argv [0] will typically point to the name of the executable, which is probably not the file you wish to parse. If you are trying to parse your currently running executable to find your particles, then you have a bad design. So make this change:

ifstream file(argv[1], ios::binary | ios::ate);

运行程序是这样的(对于Linux):

Run your program like this (for Linux):

./myprogram file.bin

或(适用于Windows):

or (for Windows):

.\myprogram.exe file.bin

如果你想调试你的程序在你的IDE

或更改默认的命令行参数。

Or change the default command line arguments in your IDE if you're trying to debug your program.

0xFF000000 不是排列成行的4字节边界上。

The value 0xFF000000 is not on an aligned four-byte boundary.

例如,该值可能为s $ P $垫。这种情况有点复杂。
你将不得不主要通过 uint32_t的阵列与的char *迭代并寻找 0xFF的,看看是否能找到3 0×00 前或(视字节序)之后。

For instance, the value could be spread across two uint32_ts in your array. This situation is a bit more complicated. You will have to basically iterate through your uint32_t array with a char* and look for a 0xFF and see if you can find 3 0x00 before or after it (depending on endianness).

这个答案剩下的只是一些小建议。如果你愿意,你可以忽略这个休息。下面的所有的这假定问题2不存在。

The rest of this answer is just some minor recommendations. You can ignore the rest of this if you want. All of this below assumes problem 2 does not exist.

下面是我如何生成的code测试文件:

Here is how I generated a test file for your code:

void createBinFile (const std::string &file_name)
{
    // Some random data to use.
    const uint32_t sample_data [] = {
        0x000000cc,
        0x0000dd00,
        0x00ee0000,
        0xff000000,
        0x00000011,
        0x00002200,
        0x00330000,
        0x44000000,
    } ;

    // Write some binary data.
    std::ofstream file (file_name, std::ios::binary) ;
    file.write (reinterpret_cast <const char *> (sample_data), sizeof (sample_data)) ;
}

管理您自己的记忆是​​不必要的。下面是使用的std ::矢量&lt办法; uint32_t的&gt;将而不是一个原始的 uint32_t的* 数组:

Managing your own memory is unnecessary. Here's a way to use a std::vector <uint32_t> instead of a raw uint32_t* array:

std::vector <uint32_t> loadBinFile (const std::string &file_name)
{
    std::ifstream file (file_name, std::ios::binary | std::ios::ate) ;

    std::streampos size = file.tellg () ;
    file.seekg (0, std::ios::beg) ;

    unsigned padding = (size % sizeof (uint32_t) == 0) ? 0 : 1 ; // or throw exception 
    unsigned vec_size = size / sizeof (uint32_t) + padding ;

    std::vector <uint32_t> data (vec_size) ;
    file.read (reinterpret_cast <char*> (&data[0]), size) ;

    return data ;
}

这里有一个快速的驱动程序,说明如何找到你的 0xFF000000 值。这需要你的#include&LT;算法方式&gt; 的std ::发现()

int main (int argc, char *argv [])
{
    const std::string file_name = argv [1] ;

    std::vector <uint32_t> data = loadBinFile (file_name) ;

    const uint32_t sentry_value = 0xff000000 ;

    typedef std::vector <uint32_t>::const_iterator const_iterator ;
    const_iterator citer = std::find (data.begin (), data.end (), sentry_value) ;

    if (citer != data.end ()) {
        std::cout << "Particle found!" "\n" ;
    }

    else {
        std::cout << "Particle not found!" "\n" ;
    }

    return 0 ;
}

这篇关于不一致的二进制数据或条件语句更改数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆