简体   繁体   English

(C ++)无法将十六进制写入.dat文件

[英](C++) Trouble writing hex to .dat file

My program is pretty long and complex so I cannot explain it all, I will try to just explain my current situation, however. 我的程序很长而且很复杂,因此我无法全部解释,但是我将尝试仅解释我的当前情况。

I have a .dat file (729 of them but only 1 for this example) and I need to overwrite THE WHOLE FILE (even if my new data is smaller than the current data) with a couple strings containing hex values. 我有一个.dat文件(其中有729个文件,但在此示例中只有1个文件),我需要用几个包含十六进制值的字符串覆盖整个文件(即使我的新数据小于当前数据)。

Here is a snippet from my program: 这是我程序中的片段:

ofstream xbox_final(xboxit->c_str(), ios::binary);

//other stuff

xbox_final.write("0a00000a00054c6576656c070006426c6f636b7300008000", 48);
xbox_final.write(new_bytes, new_bytes.length());
xbox_final.write(xboxbytes, xboxbytes.length());
xbox_final.close();

"xboxit->c_str()" in the ofstream is there because I'm inputting and outputting a BUNCH of files from a file list and this was necessary but gives me no errors, if that makes sense. ofstream中的“ xboxit-> c_str()”就在那儿,因为我正在从文件列表输入和输出BUNCH文件,这是必要的,但如果有道理,则不会给我任何错误。

The strings "new_bytes" and "xboxbytes" contains hex values (000007000a0445 etc...) 字符串“ new_bytes”和“ xboxbytes”包含十六进制值(000007000a0445等...)

For now my program has just been writing to a text document. 现在,我的程序刚刚写入文本文档。 I need it to write that hex data in that order to the .dat file. 我需要将十六进制数据按该顺序写入.dat文件。

I did some searching around and this fixes the const char problem: 我做了一些搜索,这解决了const char问题:

//other stuff

string header("0a00000a00054c6576656c070006426c6f636b7300008000");

WriteStr2BinFh( header, xbox_final);
WriteStr2BinFh( new_bytes, xbox_final);
WriteStr2BinFh( xboxbytes, xbox_final);

//other stuff

void WriteStr2BinFh( const std::string& St, std::ostream &out ) 
{
    out.write( St.c_str(), St.size() );
}

Creating new .dat files somewhere else is also an acceptable option. 在其他位置创建新的.dat文件也是一种可接受的选择。

I JUST CAN'T WRITE THE HEX VALUES, THEY ALWAYS SHOW UP AS TEXT IN THE DAT FILE. 我只是不能写十六进制值,它们总是显示为DAT文件中的文本。

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

A way to "easily" do that while keeping your current "model" would be to put "\\x" in front of every of your hex value in your string. 在保持当前“模型”的同时“轻松”执行此操作的一种方法是将"\\x"放在字符串中每个十六进制值的前面。 The compiler will then treat them as actual values, and not as a pair of characters. 然后,编译器会将它们视为实际值,而不是一对字符。

What I mean is : 我的意思是 :

std::cout << "4A4A" << std::endl;

would output : 将输出:

4A4A

whereas

std::cout << "\\x4A\\x4A" << std::endl;

would output: 将输出:

JJ

since 0x4A is the hexadecimal value for the ascii character J . 因为0x4A是ASCII字符J的十六进制值。 Of course, the implications would be the same if you were writing this string to a file. 当然,如果您将此字符串写入文件,则含义将相同。

If you want to continue using a string to store it, you'll need to change the constructor since your string will contain the value 0 . 如果您想继续使用字符串来存储它,则需要更改构造函数,因为您的字符串将包含值0

string header("your_hex_chain", number_of_bytes_in_your_chain); // \\x00 is one byte, for example

It would, however, be much easier to use a struct to store the values of your header, and write if directly to the .dat file. 但是,使用struct存储标头的值并直接将其写入.dat文件会容易得多。 But I don't know much more about what you're trying to do, so... 但是我对您要做什么的了解不多,所以...

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

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