Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对可能包含null的数据执行字符串操作\0_C++_String_Null - Fatal编程技术网

C++ 对可能包含null的数据执行字符串操作\0

C++ 对可能包含null的数据执行字符串操作\0,c++,string,null,C++,String,Null,我正在为IE实现一个可插入的MIME过滤器。这个问题涉及IIInternetProtocol::Readvoid*、ULONG、ULONG*,我正在拦截传入的HTML,以修改HTML HTML通常是UTF-8编码的,只有一些\0空字符,并且位于字符缓冲区内。我希望将其加载到std::string实例中,以便执行std::string::find等字符串操作,并通过将子字符串复制到注入字符串周围的目标缓冲区来插入内容,类似于以下内容: string received( this->buffe

我正在为IE实现一个可插入的MIME过滤器。这个问题涉及IIInternetProtocol::Readvoid*、ULONG、ULONG*,我正在拦截传入的HTML,以修改HTML

HTML通常是UTF-8编码的,只有一些\0空字符,并且位于字符缓冲区内。我希望将其加载到std::string实例中,以便执行std::string::find等字符串操作,并通过将子字符串复制到注入字符串周围的目标缓冲区来插入内容,类似于以下内容:

string received( this->buffer );
size_t index = received.find("<p id=\"foo\">");
if( index != string::npos ) {

    memcpy( destination             , received              , index );
    memcpy( destination + index     , "Injected content"    ,    17 );
    memcpy( destination + index + 17, received.substr(index), received.size() - 17 - index );
} else {
    memcpy( destination             , this->buffer          , this->bufferSize );
}

问题是缓冲区可能包含空字节,这是我正在使用的网站的一个怪癖。\0字符值与字符串操作(如查找)的交互程度如何?MSDN或CPlusPlus.com上的文档没有说明。

如果收到的文本是纯文本并经过编码,则不应包含任何零字节。如果是,则文本不是UTF-8。虽然std::string可以使用嵌入的零来存储数据,但您必须使用另一种方法来指定长度,我不确定您是否真的可以将其视为字符串,然后使用正常的字符串操作,您可以自己尝试:std::string函数不应该关心字符串中的字符-我不确定标准是否这样说,但根据我的经验,字符串中的NUL字符没有任何影响,除非您使用c_str。