2012-11-21 104 views
0

我正在實現一個小型搜索引擎。該信息是從文件中獲取的,並且包含該詞的所有搜索都會返回。問題是當我在while循環中運行搜索函數時,一個分段錯誤的錯誤,但當我運行它沒有一個while循環時,它運行順利..我已經重新檢查了代碼不少次,但無法找到任何問題。任何幫助,將不勝感激。由於while循環導致的分段錯誤錯誤

//searches individual words which are given to it.array has some data in it 
int search_word(string word,string array) 
{ 
    int counter=0; 
    size_t found; 
    int i=0; 
    for (i=array.find(word,0);i!=string::npos;i=array.find(word,i)) 
    { 
     counter+=1; 
     i++; 
    } 
    return counter; 
} 



//this is sorting(descending) the linked list of results and storing it in an array 
void Rank() 
{ 
    struct results *go; 
    go=head; 
    int array[54]; 
    int l=0; 
    while (go->next!=0) 
    { 
     array[l]=go->number; 
     array1[l]=go->result; 
     go=go->next; 
     ++l; 
    } 
    array[l]=go->number; 
    array1[l]=go->result; 
    int temp; 
    string temp1; 
    for (int k=0;k<l;k++) 
    { 
     for (int j=(k+1);j<l+1;j++) 
     { 
      if (array[k]<array[j]) 
      { 
      temp=array[k]; 
      array[k]=array[j]; 
      array[j]=temp; 
      temp1=array1[k]; 
      array1[k]=array1[j]; 
      array1[j]=temp1; 
      } 
     }     
    } 
    cout<<"Following results were found"<<endl; 
    for (int a=0;a<l;++a) 
    { 
     cout<<"Result no."<<a<<endl; 
     cout<<array1[a]<<endl; 
     cout<<endl; 
    } 
} 

//main search function     
void search(string array[]) 
{ 
    //words like the,a,he,they,would are low rank words 
    ifstream fin; 
    fin.open("lowRank.txt"); 
    string lowrank; 
    int j=0; 
    while (!fin.eof()) 
    { 
     string buffer; 
     do 
     { 
      getline(fin,buffer); 
      lowrank+=" "; 
      lowrank+=buffer; 
      ++j; 
     } 
     while (!buffer.empty()); 
    } 
    fin.close(); 
    cout<<"Enter a word or phrase"<<endl; 
    //converting the string containing words into seperate strings by tockenisation 
    string setofwords[10]; 
    char phrase[50]; 
    cin.get(phrase,50); 
    int count=0; 
    char *pointer= NULL; 
    pointer=strtok(phrase," "); 
    while (pointer!=NULL) 
    { 
     char *pointer= NULL; 
     setofwords[count]=pointer;  
     pointer=strtok(NULL," "); 
     count+=1; 
     delete [] pointer; 
    }  

    for (int i=0;i<54;i++) 
    { 
     int counter=0;int counter1=0; 
     //searching for all words one by one in the below loop  
     for (int k=0;k<count;k++)  
     {   
      //searching if word is low rank 
      size_t found; 
      found=lowrank.find(setofwords[k]);     
      if (found!=string::npos) 
      { 
       counter=search_word(setofwords[k],array[i]); 
       if (counter>0) 
       {      
        counter1+=1; 
       } 
      } 
      else 
      {           
       counter=search_word(setofwords[k],array[i]); 
       counter1+=counter;     
      }   
     } 
    if (counter1>0) 
    { 
     add_result(array[i],counter1); 
    } 
    } 
    Rank(); 
} 

int main() 
{ 
    //reading the data file and copying it into an a linked list 
    ifstream fin; 
    fin.open("data.txt"); 
    while (!fin.eof()) 
    {  
     string url; 
     string data; 
     getline(fin,url); 
     string buffer; 
     do 
     { 
      getline(fin,buffer);   
      if (!buffer.empty()) 
      { 
       data=buffer; 
      } 
     } 
     while (!buffer.empty()); 
     add_node(url,data); 
    } 
    //copying linked list to an array 
    string array[54]; 
    node *conductor; 
    conductor=root; 
    for (int i=0;i<54;i++) 
    { 
     array[i]=conductor->url+conductor->data; 
     conductor=conductor->next; 
    }     

    fin.close(); 

    while (1) 
    {    


     cout<<"*******************************SEARCH         
     ENGINE********************************"<<endl; 
     cout<<endl; 
     cout<<endl; 
     cout<<endl; 
     cout<<"1.Press 1 to search a word."<<endl; 
     cout<<"5.Press 5 to quit without saving search results"<<endl; 

    int ans; 

     cin>>ans;  
     if (ans==1) 
     {  
      search(array); 
      search(array); 
     } 

     else if (ans==5) 
     { 
      quit_without_saving(); 
     } 
     } 

system("PAUSE"); 
return 0; 
} 
+1

'while(!fin.eof())'是錯誤的。 http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong –

+0

問題不在於此while循環,而是最後一個。但感謝您指出。 – User14229754

+0

那你爲什麼要發佈整件事? –

回答

3

分割故障可能是由於delete []上的指針的使用不是由一個調用返回到new[]:沒有張貼在代碼中的關鍵字new的一個實例,但有一個delete的實例。

+0

即使我刪除了刪除命令,問題仍然存在。 – User14229754

+0

@ User14229754現在是您通過調試器運行代碼並嘗試將問題隔離到一小段代碼的時候。我懷疑有人會替你幹這個。 –

3

你的錯誤是在這行代碼:

char phrase[50]; 
cin.get(phrase,50); 
int count=0; 
char *pointer= NULL; 
pointer=strtok(phrase," "); 
while (pointer!=NULL) 
{ 
    char *pointer= NULL; 
    setofwords[count]=pointer;  
    pointer=strtok(NULL," "); 
    count+=1; 
    delete [] pointer; 
} 

strtok實際上返回您在你通過什麼,在這種情況下phrase指向一個字符。

phrase在堆棧上創建。

因此,您不能致電delete[]就可以了。順便提一下,你也不能在分配的塊的中間調用delete[],它必須恰好是從new[]調用返回的指針。

順便說一句,如果你需要改變你的代碼,這樣phrase需要知道它在運行時的大小,不要用new[]但短語做成std::vector<char>相反,您可以使用&phrase[0]去它的第一個元素。再次,不需要撥打delete[]

+0

但短語必須存儲幾個字,然後我必須access.shouldn't它是標準::矢量 ??對不起,我是新的矢量。 – User14229754

+0

你應該將它們解析成矢量。請注意,有多種方法可以在C++中執行此操作,而不是使用strtok,例如istream_iterator或boost :: tokenize,它們是可重入的。 (因爲它保存狀態,strtok不會在多個線程中工作)。 – CashCow