2015-12-21 133 views
-3

我在这里构建webcrawler。我开始调试时发生此错误,并将我发送到memcpy.asmxstringdbgdel.cpp文件,每次显示这些文件的不同行。C++访问冲突写入位置0x000A000B

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

这个想法是遍历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; 
} 

UPDATE

我编辑了=,现在==

我也停止迭代7位早先所以“如果公司不应该是问题。

虽然我收到相同的错误。

+5

你即使那些严重'如果公司使用'='的'而不是'== –

+1

您的所有'INT I = 0;我<= httpContent.length()'好吧,这可能不是很好,因为'httpContent [i]'将在最后一次迭代中超出范围....'c = httpContent [i + 5];',嗯 – NathanOliver

+2

if's – AndyG

回答

1

使用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; 
}