Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ while循环导致的分段错误_C++ - Fatal编程技术网

C++ while循环导致的分段错误

C++ while循环导致的分段错误,c++,C++,我正在实现一个小型搜索引擎。信息取自一个文件,所有包含单词的搜索都会返回。问题是,当我在while循环中运行搜索函数时,会出现分段错误,但当我在没有while循环的情况下运行时,它运行得很顺利。我已经重新检查了好几次代码,但没有发现任何问题。如果有任何帮助,我们将不胜感激 //searches individual words which are given to it.array has some data in it int search_word(string word,string a

我正在实现一个小型搜索引擎。信息取自一个文件,所有包含单词的搜索都会返回。问题是,当我在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;
}
//搜索给定给它的单个单词。数组中有一些数据
int搜索_字(字符串字、字符串数组)
{
int计数器=0;
未找到尺寸;
int i=0;
for(i=array.find(word,0);i!=string::npos;i=array.find(word,i))
{
计数器+=1;
i++;
}
返回计数器;
}
//这是对结果链表进行排序(降序)并将其存储在数组中
无效等级()
{
结构结果*go;
go=头部;
int数组[54];
int l=0;
while(前进->下一步!=0)
{
数组[l]=go->number;
数组1[l]=开始->结果;
go=go->next;
++l;
}
数组[l]=go->number;
数组1[l]=开始->结果;
内部温度;
字符串temp1;

对于(int k=0;k分段错误可能是由于在调用
new[]时未返回的指针上使用了
delete[]
:在发布的代码中没有一个关键字
new
的实例,但是有一个
delete

分段错误可能是由于在未通过调用
new[]返回的指针上使用了
delete[]
:在发布的代码中没有一个关键字
new
的实例,但是有一个
delete

的实例。您的错误在以下代码行中:

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

已在堆栈上创建短语

因此,您不能在其上调用
delete[]
。顺便说一句,您也不能在已分配块的中间调用
delete[]
,它必须恰好是从
new[]
调用返回的指针


顺便说一句,如果您需要更改代码,以便
短语
需要在运行时知道其大小,请不要使用
new[]
,而是将短语转换为
std::vector
,您可以使用
&短语[0]
访问其第一个元素。再次,无需调用
delete[]

您的错误在以下代码行中:

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

已在堆栈上创建短语

因此,您不能在其上调用
delete[]
。顺便说一句,您也不能在已分配块的中间调用
delete[]
,它必须恰好是从
new[]
调用返回的指针


顺便说一句,如果您需要更改代码,以便
短语
需要在运行时知道其大小,请不要使用
new[]
,而是将短语转换为
std::vector
,您可以使用
&短语[0]
访问其第一个元素。再次,无需调用
delete[]
,而(!fin.eof())
是错误的。问题不在这个while循环中,而在最后一个while循环中。但是谢谢你指出。那么你为什么要发布整个内容呢?我不确定分段错误在最后一个while循环中的什么位置,我调用了搜索函数,但是while(!fin.eof))工作得非常好。
while(!fin.eof())
是错误的。问题不在这个while循环中,而是在最后一个while循环中。但是感谢您指出。那么您为什么要发布整个内容?我不确定分段错误在最后一个while循环中的什么位置,我调用了搜索函数,但while(!fin.eof)工作正常。@User14229754现在是您通过调试器运行代码并尝试将问题隔离到一小段代码的时候。我怀疑有人会替您这么做。@User14229754现在是您通过调试器运行代码并尝试将问题隔离到一小段代码的时候。我怀疑有人会这样做在你的Stand中,你会很难做到这一点,但是短语必须存储几个我必须访问的词。难道它不是STD::vector?对不起,我是新的向量。你应该把它们解析成向量。注意C++中有很多方法来做这件事,而不是使用Strutok,例如ISTRAMAMIATER或Booo::(Sttok在多个线程中不工作,因为它保存状态)。但是短语必须存储几个单词,然后我必须访问。它不应该是STD::vector?对不起,我是新的向量。你应该把它们解析成向量。注意有很多方法在C++中使用,而不是使用Strutok,例如istRAMMyIdor或Booo:::(strtok无法在多个线程中工作,因为它保持状态)。