(c++) Read .dat file as hex using ifstream

扶醉桌前 提交于 2019-12-02 11:08:07

问题


This is my first post so sorry if I break any unwritten rules. :P I am a beginner/intermediate programmer and I need help with this program.

I am trying to infile/read/ifstream (whatever) a .dat file as HEX into one big string.

I don't want to read it as text. I want the hex format so I can search through the string and make changes to it. (Like an automatic hex editor)

ex. my file "00000000.dat" is ~7kb in size.

In hex editor, the hex look like this:

0A 00 00 0A 00 05 4C 65 76 65 6C 07 00 06 42 6C 6F 63 6B 73 00 00 80 00 07 FF 39
01 FF 03 03 02 FF 3F 00 07 FF 39 01 FF 03 03 02 FF 3F 00 07 FF 39 01 FF 03 03 02
FF 3F 00 07 FF 39 01 FF 03 03 02 FF 3F 00 07 FF 39 01 FF 03 03 02 FF 3F 00 07 FF
39 01 FF 03 03 02 FF 3F 00 07 FF 39 01 FF 03 03 02 FF 3F 00 07 FF 39 01 FF 03 03
02 FF 3F 00..... for a while...

I need all of it in a string variable (with no spaces preferably).

My current code sucks and for now only prints the result. (got it from ehow) and seems to pick and choose what it wants to input/print.

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    ifstream input;
    input.open("00000000.dat");

    unsigned char h1,h2;

    do
    {
        input >> h1;
        cout << hex << (unsigned int)h1;
        input >> h2;
        cout << hex << (unsigned int)h2;
    }while(!input.eof());

cin.get();
return 0;
}

It's a big file so I can't show yo what it prints, but it is missing some bytes. (ex "0A 00 00 0A 00 05....." prints as "00 05.....") this is true for the ending as well.

Sorry if I didn't explain it well :(


回答1:


You should open the stream as binary, as mentioned. You can use the regular >> operator if you tell it not to skip white space.

unsigned char x;
std::ifstream input("00000000.dat", std::ios::binary);
input >> std::noskipws;
while (input >> x) {
    std::cout << std::hex << std::setw(2) << std::setfill('0')
              << (int)x;
}

To get the content into a string, you can use an ostringstream instead of cout.




回答2:


You want to open the file to read as binary. You're reading as text right now. So it should look like

//Open file object to read as binary
std::ifstream input("00000000.dat", std::ios::in | std::ios::binary);    

You also might want to use reinterpret_cast to read one byte at a time (i.e. can't figure out what you want -- your code and your description are doing opposite things).

//Read until end of file
while(!input.eof())    
{

    //Or .good() if you're paranoid about using eof()

    //Read file one byte at a time
    input.read(reinterpret_cast<char *>(&h1), sizeof(unsigned char));

}

Or if you just want everything in one string, why not just declare a string and read it into that?



来源:https://stackoverflow.com/questions/16410362/c-read-dat-file-as-hex-using-ifstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!