我正在制作一个程序,其目的之一就是它需要能够根据字符将输入分隔成不同的字符串。 例如,输入后5秒程序崩溃?
game.Lighting被分离成“游戏”和“照明” game.Properties.Hey被分离成“游戏”和“属性”,“嘿” 基本上,它只是删除了。并将它们分成不同的字符串。
然而,当我输入我希望字符串是,它的工作原理以及它是否已完全正常,然后崩溃约5秒后出现错误“字符串str已损坏”
这是我的代码。
#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
char str[2] = ".";
cin >> str;
char * pch;
printf("Splitting string \"%s\" into tokens:\n", str);
pch = strtok(str, ".");
while (pch != NULL)
{
printf("Getting %s then ", pch);
pch = strtok(NULL, ".");
}
Sleep(5000);
return 0;
}
我改变了char str [] =“。”以char str [2] =“。”
不过,我现在得到的错误(这是后该程序已编译并运行,然后它崩溃)“字符串str已损坏”
你忘了把你的照明放入轨道。 –
什么@LightnessRacesinOrbit – JStep