C++ 如何从缓冲区读取特定字符串

C++ 如何从缓冲区读取特定字符串,c++,c,arrays,string,buffer,C++,C,Arrays,String,Buffer,我有一个缓冲区 char buffer[size]; 我正在使用它来存储流的文件内容(假设这里是pStream) 现在我把这个流的所有内容都放在缓冲区中,缓冲区大小(假设这里的大小)。现在我知道我有两条弦 "<!doctortype html" and ".html>" “” 它们存在于这个缓冲区存储内容的某个地方(我们不知道它们的位置),我只想存储这个位置的缓冲区内容 "<!doctortype html" to another string ".html>"

我有一个缓冲区

char buffer[size];
我正在使用它来存储流的文件内容(假设这里是pStream)

现在我把这个流的所有内容都放在缓冲区中,缓冲区大小(假设这里的大小)。现在我知道我有两条弦

"<!doctortype html" and ".html>" 
“”
它们存在于这个缓冲区存储内容的某个地方(我们不知道它们的位置),我只想存储这个位置的缓冲区内容

"<!doctortype html" to another string ".html>"  
“”
进入另一个缓冲区2[SizeWeDontKnow]尚未


怎么做???(实际上,这两个位置的内容是html文件的内容,我只想存储缓冲区中存在的html文件的内容)。有什么办法吗???

寻找C/C++的HTML解析器


另一种方法是从缓冲区的开始处设置一个字符指针,然后在其后检查每个字符。看看它是否符合你的要求。

< P>你是否限于C,或者你可以使用C++?< /P> 在C库参考中,有很多有用的方法可以标记字符串并比较匹配项(string.h):

使用C++我会做以下操作(使用代码中的缓冲区和大小变量):


您可以使用strnstr函数在缓冲区中找到正确的位置。找到开始和结束标记后,可以使用strncpy提取中间的文本,如果性能有问题,可以就地使用。

您可以根据标记的位置和第一个标记的长度计算所需的大小
如果这是应用程序中对HTML代码进行操作的唯一操作,那么您可以使用下面提供的解决方案(您也可以在线测试它)。但是,如果您要进行更复杂的解析,那么我建议使用一些外部库

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int main()
{
    const char* beforePrefix = "asdfasdfasdfasdf";
    const char* prefix = "<!doctortype html";
    const char* suffix = ".html>";
    const char* postSuffix = "asdasdasd";

    unsigned size = 1024;
    char buf[size];
    sprintf(buf, "%s%sTHE STRING YOU WANT TO GET%s%s", beforePrefix, prefix, suffix, postSuffix);

    cout << "Before: " << buf << endl;

    const char* firstOccurenceOfPrefixPtr = strstr(buf, prefix);
    const char* firstOccurenceOfSuffixPtr = strstr(buf, suffix);

    if (firstOccurenceOfPrefixPtr && firstOccurenceOfSuffixPtr)
    {
        unsigned textLen = (unsigned)(firstOccurenceOfSuffixPtr - firstOccurenceOfPrefixPtr - strlen(prefix));
        char newBuf[size];
        strncpy(newBuf, firstOccurenceOfPrefixPtr + strlen(prefix), textLen);
        newBuf[textLen] = 0;

        cout << "After: " << newBuf << endl;
    }

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
const char*beforePrefix=“asdfasdfasdfasdf”;
常量字符*前缀=”;
const char*postSuffix=“asdasdasd”;
无符号大小=1024;
字符buf[大小];
sprintf(buf,“%s%s要获取的字符串%s”,前缀前、前缀前、后缀后);

我对第二个想法很感兴趣,但是你能告诉我,通过从一开始就使用字符指针,我一次只能比较一个字符,但是我必须比较一个大小等于**"<代码>缓冲区<代码>是指向缓冲区的指针。现在创建另一个char指针,作为<代码> char *b=和缓冲器。现在B指向缓冲区的开始,可以移动这个缓冲区而不丢失缓冲区。现在,如果缓冲区中的第一个字符是“我使用Visual C++”。你认为Strutok对我有好处吗?因为我必须搜索“的位置”,然后把内容存储在它们之间。你有什么建议?我已经用C++测试过的更新了我的答案,我用G+编译器测试:G++(GCC)4.4.7 20120313(红帽4.4.7到3)。。让我知道它是否适合你。很高兴你得到了答案,非常感谢,西蒙。如果你想查看此链接,我已经编写了查找包含特定文件的文件的解决方案。html“在缓冲区/流中,如果不知道前缀和后缀的位置,将来可能会对您有用。我无法获取它们之间的数据,因此“您要获取的字符串”不可能,我想你还是不能理解我的问题。没错,我错过了这个细节。我刚刚更新了答案:“你有没有在Visual C++中找到位置的想法?”CWE可以使用StnSTR,但是这里不……有什么想法?代码还是一样的。tart=strstr(buf,前缀)-buf;int-nPosEnd=strstr(buf,后缀)-buf;int-yourTextLen=nPosEnd-nPosStart-strlen(前缀)在以前的编辑中,我只更新了链接,现在代码被更新了。如果你有任何想法,Visual C++支持STRNSTR函数?什么是nStTaTaGrand?我们只需要这个,为什么你使用NSTATTAGGLUTHOND??Visual C++没有STRNSTR,但是如果你不确定读函数给你一个零终止的字符串,你可以把它本身放在零中:<代码> char缓冲器[大小+1 ];ZoMeMeRoice(缓冲区,大小+1);< /CordsNPOSCAST和NPOSEND是开始标记的位置”。NStuttAgStand的长度是“你知道Visual C++的等价函数吗?”,你认为我可以用Strutok()来计算它吗?- -(pCH!= null){pCH=Strutok(null),“DOCTRORYPE HTML”“返回0;}我可以得到”DOCTURTYPE HTML的位置!“使用这个?如果缓冲区为零,可以使用STRSTR。如果不是,可以使它为零终止,就像我在以前的注释中所解释的那样。查找C++引用。它不使用像STRNSTR那样的大小参数。请在使用之前学习工具。
    // copy char array to std::string
    std::string text(buffer, buffer + size);

    // define what we're looking for
    std::string begin_text("<!doctortype html");
    std::string end_text(".html>");

    // find the start and end of the text we need to extract
    size_t begin_pos = text.find(begin_text) + begin_text.length();
    size_t end_pos = text.find(end_text);

    // create a substring from the positions
    std::string extract = text.substr(begin_pos,end_pos);

    // test that we got the extract
    std::cout << extract << std::endl;
char* tmp =  extract.c_str();
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int main()
{
    const char* beforePrefix = "asdfasdfasdfasdf";
    const char* prefix = "<!doctortype html";
    const char* suffix = ".html>";
    const char* postSuffix = "asdasdasd";

    unsigned size = 1024;
    char buf[size];
    sprintf(buf, "%s%sTHE STRING YOU WANT TO GET%s%s", beforePrefix, prefix, suffix, postSuffix);

    cout << "Before: " << buf << endl;

    const char* firstOccurenceOfPrefixPtr = strstr(buf, prefix);
    const char* firstOccurenceOfSuffixPtr = strstr(buf, suffix);

    if (firstOccurenceOfPrefixPtr && firstOccurenceOfSuffixPtr)
    {
        unsigned textLen = (unsigned)(firstOccurenceOfSuffixPtr - firstOccurenceOfPrefixPtr - strlen(prefix));
        char newBuf[size];
        strncpy(newBuf, firstOccurenceOfPrefixPtr + strlen(prefix), textLen);
        newBuf[textLen] = 0;

        cout << "After: " << newBuf << endl;
    }

    return 0;
}