p周围的堆栈已损坏c ++

cap*_*onk 1 c++ string char

它一定很简单,但我现在很麻木,想清楚.所以这只是我代码的一小部分,除此之外一切正常.我真正想做的是infile1.open(temp2-> path); 但由于temp2是一个字符串,所以不可能.所以我想将这个字符串放入char数组[100]中的char数组中,以使用infile1.open(p).它编译但经过几秒钟的繁荣:p周围的堆栈已损坏

   char p[100];
   while( temp2 != 0)
   {
        stringToCharArray(temp2->path, p); 
        infile1.open(p);
        checkingWords(infile1, stopWords, invertedIndex);
        infile1.close();
        temp2 = temp2->next;
   }


void stringToCharArray(string s, char *c)
{
    int i;
    int size = s.size();
    for( i=0; i<=size ; i++)
    {
        c[i] = s[i];
    }
}
Run Code Online (Sandbox Code Playgroud)

BoB*_*ish 6

我相信你想要的std::string::c_str.

infile1.open(temp2->path.c_str());
Run Code Online (Sandbox Code Playgroud)

(小心,这const char *只有在你下次改变某些东西时才有效std::string,在这种情况下temp2->path).

它看起来for( i=0; i<=size ; i++)应该是您现有代码的主要错误i<size.但是,我们不要过多地详述,因为你不应该这样做.