gpt4 book ai didi

c++ - strstr 无法以八进制格式搜索数据中的字符串

转载 作者:行者123 更新时间:2023-11-28 03:12:55 24 4
gpt4 key购买 nike

我试图在给定文件中找到一个字符串(实际上该文件是 tar 文件(请注意此处),我在记事本++ 中打开该文件并从该打开的文件中随机取出一个字符串)并且我存储了完整的 tar缓冲区中的文件,现在我想在存储的缓冲区中找到我使用 strstr 函数复制的字符串的位置。

代码是这样的(绝对正确)-

char *compare= "_.png"; //suppose this is the copied string
//which is to be find out in buffer using strstr
char * StartPosition;
StartPosition = strstr (buffer,compare);
__int64 count=0;
MessageBox(m_hwndPreview,L"before the while loop",L"BTN WND6",MB_ICONINFORMATION);
while (StartPosition!=NULL)
{
MessageBox(m_hwndPreview,L"hurr inside the while loop",L"BTN WND6",MB_ICONINFORMATION);
MessageBoxA(m_hwndPreview,strerror(errno),"BTN WND4", MB_ICONINFORMATION);
count=StartPosition-buffer+1;
return 0;
}

假设我在记事本中有 tar 文件的内容,如下所示,从我复制存储在比较中的字符串的地方-

3_VehicleWithKinematicsAndAerodynamics_.000.png  IHDR (here is some strange data which can't be copied and also there are lot of NULL but we have to find out the position of "_.png" so not so difficult in this case ).

问题是我的代码工作正常,直到我将数据存储在 .png 之前,然后我能够使用 strstr 找到它的位置问题是当我试图找出出现在

之后的字符串位置时
`3_VehicleWithKinematicsAndAerodynamics_.000.png  IHDR ...suppose here we have strange data (which is data block if we see the tar parser)...after this we have another file  like..."3_VehicleWithKinematicsAndAerodynamics_.html"`

如果我想使用 strstr 找到这个“3_VehicleWithKinematicsAndAerodynamics_.html”,那么我无法找到它,因为它们之间有奇怪的数据。(因为我认为这些数据无法被编译器识别并且无法识别我无法访问位于奇怪数据之后的文件)为了更清楚地看到文件在 tar 文件中的位置如下-

3_VehicleWithKinematicsAndAerodynamics_.000.png  IHDR ............(its data blocl-which is strange contents if you open in tar file)....3_VehicleWithKinematicsAndAerodynamics_.000.html

我必须使用 strstr 访问 .html 文件。为什么它不访问它??有任何想法吗 ?? *

请提供实现它的替代方法..我确信我尝试的方法不会起作用..

最佳答案

C 风格字符串是一些以零字符结尾的字符(NUL 字符 - 值零,而不是字符“0”)。这意味着 strstr 将在遇到这样的字节时立即停止。

一个相当合理的解决方案是简单地编写一个函数,根据二进制数据的长度而不是“终止符”来搜索二进制数据。

像这样(这仍然假定 str 是 C 风格的字符串):

 char *find_str_in_data(const char *str, const char *data, int length)
{
int pos = 0;
int slen = strlen(str);
while(pos < length-slen)
{
int i = 0;
while(i < slen && str[i] = data[pos+i])
{
i++;
}
if (i == slen)
return data + pos;
}
return NULL;
}

关于c++ - strstr 无法以八进制格式搜索数据中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17882558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com