我正在嘗試visual C++中的strtok,但它似乎不工作。這不是我第一次使用strtok,但我只是想知道它有什麼問題。代碼很簡單。strtok visual C++中的問題
在主cout<<getLevels("/'Group'/'Channel1'")<<endl;
在getLevels()
int getLevels(char * fullPath){
int level=0;
char *nextToken;
char * pch=strtok_s(fullPath, "/", &nextToken);// broken at here
while(pch!=NULL){
level++;
cout<<level<<":"<<pch<<endl;
pch=strtok_s(NULL, "/",&nextToken);
}
return level;
}
它打破在線路
char * pch=strtok_s(fullPath, "/", &nextToken);
,錯誤:
Unhandled exception at 0x10273de8 (msvcr100d.dll) in tdmsTest.exe: 0xC0000005: Access violation writing location 0x0041c840.
並且光標指向該線在strtok_s.ini
for (; *str != 0 ; str++)
{
if (map[*str >> 3] & (1 << (*str & 7)))
{
*str++ = 0; // pointing here
break;
}
}
我嘗試了在的strtok()而不是strtok_s()之前,但它具有相同的問題。 任何人都可以告訴我我的代碼有什麼問題嗎?
謝謝。你是對的。我改成\t char * temp = strdup(「/'Group'/'Channel1'」); \t cout << getLevels(temp)<< endl;它的工作原理是 。 謝謝 – Grey 2010-07-02 16:05:24