WinInet InternetReadFile 从缓冲区中获取行 ( ) 并插入向量

问题描述 投票:0回答:0

我正在寻找一个很好的解决方案,将缓冲区内容拆分成行(搜索

\n
)并逐行插入 Vector。我用
str.find
str.substr
试过了。

恐怕一旦它再次从网络上读取新内容(因为缓冲区已满,现在它再次从网络上读取内容),它可能会丢失上次读取的最后一点内容。有人上次阅读的不完整行必须贴在下一次阅读的前面。

我试过这样的东西,但是,它实际上并没有像我需要的那样工作。 我期待任何人有一个很好的解决方案将缓冲区内容拆分成行以将行插入向量中。

std::vector<string> szLines;
dwKeepReading = true;
dwBytesRead = -1;
while(dwKeepReading && dwBytesRead != 0)
{
    dwKeepReading = InternetReadFile(_RequestFunction_Here_, _Buffer_Here_, 16384, &dwBytesRead);

    if(dwBytesRead)
    {
        size_t szPos = 0;
        size_t szPrev = 0;
        while((szPos = string(_Buffer_Here_).find("\n", szPrev)) != string::npos)
        {
            szLines.push_back(string(_Buffer_Here_).substr(szPrev, szPos));
            szPrev = szPos + 1;
        }
        //Save somehow remaining content of buffer (if there ist any?) -> Add if at the front of the next Read to complete the line.
        szLines.push_back(string(_Buffer_Here_).substr(szPrev)); //This line might be incomplete -> Save and set on front of next read.
    }
    else
    {
        //Somehow check if there was anything remaining from the last Read to insert into the Vector.
    }
}

此刻我得到了一个肮脏的解决方法,只需将缓冲区写入缓存文件,然后将每行的文件行读入 Vector。但是,我希望直接将缓冲区拆分为行并插入 Vector 而无需编写文件。

c++ vector wininet
© www.soinside.com 2019 - 2024. All rights reserved.