简体   繁体   English

从文本文件中读取字符数组

[英]Reading a char array from a text file

I have a function:我有一个功能:

uintptr_t FindPattern(HANDLE hProcess, uintptr_t start, uintptr_t end, char *pattern, char *mask);

When I call it like this, it's OK:当我这样称呼它时,就可以了:

uintptr_t found = FindPattern(hProcess, START, END, "\x89\x41\x24\xE9\x00\x00\x00\x00\x8B\x46\x00\x6A\x00\x6A\x00\x50\x8B\xCE\xE8", "xxxx????xx?xxxxxxxx");

Now, I'm storing pattern and masking in a text file, reading these as string and then convert them back to char, but it's no longer working:现在,我将模式和掩码存储在文本文件中,将它们作为字符串读取,然后将它们转换回字符,但它不再起作用:

char* tmp1 = new char[pattern.length() + 1];
strncpy(tmp1, pattern.c_str(), pattern.length());
tmp1[pattern.length()] = '\0';

char* tmp2 = new char[mask.length() + 1];
strncpy(tmp2, mask.c_str(), mask.length());
tmp2[mask.length()] = '\0';

uintptr_t found = FindPattern(hProcess, START, END, tmp1, tmp2);

delete[] tmp1;
delete[] tmp2;

For what I see, mask is OK but I got a problem with pattern.就我所见,面具还可以,但我的图案有问题。

I think I have to suppress "\\" or maybe doubling them ("\\\\").我想我必须抑制“\\”或将它们加倍(“\\\\”)。

The problem is that "\\x89\\x41\\x24\\xE9\\x00\\x00\\x00\\..." is a notation for a string literal in C++ source code.问题是"\\x89\\x41\\x24\\xE9\\x00\\x00\\x00\\..."是 C++ 源代码中字符串文字的表示法。 This notation only has special meaning when it is part of the source code.此符号仅在作为源代码的一部分时才具有特殊含义。 The compiler interprets it as a sequence of bytes with value 0x89 , 0x41 , etc.编译器将其解释为具有值0x890x41等的字节序列。

If you copied this as is to a text file, what you really have in the file is this sequence of bytes: \\ , x , 8 , 9 , \\ , x , 4 , etc如果将其按原样复制到文本文件中,则文件中真正包含的是以下字节序列: \\x89\\x4

If the byte sequence that you want is not valid text, you cannot store it in a text file.如果所需的字节序列不是有效文本,则无法将其存储在文本文件中。 You will have to make a binary file with for instance a hex editor, or you should choose a text representation and convert it when you read it in.您必须使用例如十六进制编辑器制作一个二进制文件,或者您应该选择一种文本表示形式并在读入时进行转换。

You could for instance represent it as integers separated by spaces:例如,您可以将其表示为由空格分隔的整数:

137 65 36 233

And then read it in with:然后用以下命令阅读它:

std::string result;
std::fstream myfile("D:\\data.txt", std::ios_base::in);

int a;
while (myfile >> a)
{
    result += static_cast<char>(a);
}

std::cout << result << std::endl;

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

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