C++访问冲突写入位置0x000A000B

C++ Access violation writing location 0x000A000B

本文关键字:位置 0x000A000B 访问冲突 C++      更新时间:2023-10-16

我在这里构建这个网络爬虫。当我开始调试并将我发送到memcpy.asmxstringdbgdel.cpp文件时,我会出现此错误,每次都会显示这些文件的不同行。

我想知道代码是否有错。我开始认为我正在访问不该访问的内存块。这是一些代码。我希望你能帮忙。

这个想法是通过httpContent进行迭代,并从<a>标签中获取所有URL。我正在寻找一开始的href=",然后寻找下一个"。介于两者之间的内容我试图放入temp,然后将temp的内容传递给字符串数组。

struct Url
{
    string host;
    string path;
};
int main(){
    struct Url website;
    string href[100];
    website.host = "crawlertest.cs.tu-varna.bg";
    website.path = "";
    string httpContent = downloadHTTP(website);

for(unsigned int i = 0; i <= httpContent.length()-7; i++){
        char c = httpContent[i];
                if(c == 'h'){
                    c = httpContent[i+1];
                    if(c == 'r'){
                        c = httpContent[i+2];
                        if(c == 'e'){
                            c = httpContent[i+3];
                            if(c == 'f'){
                                c = httpContent[i+4];
                                if(c == '='){
                                    c = httpContent[i+5];
                                    if(c == '"'){
                    i+=6;
                    c = httpContent[i];
                    string temp = "";
                    while(c!='"'){
                    i++;
                    c = httpContent[i];
                    temp+= c;
                }
                href[i] = temp;
                temp = "";
                cout<<href[i]<<endl;
                                    }}}}}}
    }
    system("pause");
    return 0;
}

更新

我编辑了=,现在是==

我还提前7个位置停止迭代,所以"如果"应该不是问题。

不过我也犯了同样的错误。

使用std::vector< std::string > href;存储结果。使用string::find可以在字符串中找到序列,使用string::substr可以从字符串中提取序列。

#include <vetor>
#include <string>
struct Url
{
    string host;
    string path;
};
int main(){
    struct Url website;
    website.host = "crawlertest.cs.tu-varna.bg";
    website.path = "";
    std::string httpContent = downloadHTTP(website);
    std::vector< std::string > href;
    std::size_t pos = httpContent.find("href="); // serach for first "href="
    while ( pos != string::npos )
    {
        pos = httpContent.find( '"', pos+5 ); // serch for '"' at start
        if ( pos != string::npos )
        {
            std::size_t posSt = pos + 1;
            pos = httpContent.find( '"', posSt ); // search for '"' at end
            if ( pos != string::npos )
            {
                href.push_back( httpContent.substr( posSt, pos - posSt ) ); // extract ref and append to result 
                pos = httpContent.find( "href=", pos+1 ); // search for next "href="
            }
        }
    }
    system("pause");
    return 0;
}