更新
我以为stoi(string)解决了它,但是它只工作了一段时间。
我已经添加了splitString的代码并在下面解密。
我偶尔会使用应该使用的相同值来获取atoi()的未处理异常。
我的代码如下所示:
ifstream myfile ("Save.sav");
string line = "";
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
}
myfile.close();
line = StaticFunctions::decrypt(line);
}
vector<string> splitString = StaticFunctions::splitString(line, 's');
return atoi(splitString[0].c_str());
因此,它要做的是读取一个保存文件,然后将其解密,然后按每个“ s”分割字符串。调试时,保存文件始终相同,第一个值为3。
有时,可能每10次尝试一次。因此,每10次尝试中的9次,我都会在内存位置的...处收到未处理的异常。
监视转换后的值表明它总是返回3,然后应用程序不会崩溃,直到我启动游戏为止,这在代码中更进一步。
如果我删除atoi并仅返回3,则该应用程序可以正常运行。
我尝试过strtod,但没有帮助。
谢谢,
马库斯
SplitString代码:
vector<string> StaticFunctions::splitString(string str, char splitByThis)
{
vector<string> tempVector;
unsigned int pos = str.find(splitByThis);
unsigned int initialPos = 0;
// Decompose statement
while( pos != std::string::npos ) {
tempVector.push_back(str.substr( initialPos, pos - initialPos + 1 ) );
initialPos = pos + 1;
pos = str.find(splitByThis, initialPos );
}
// Add the last one
tempVector.push_back(str.substr(initialPos, std::min(pos, str.size()) - initialPos + 1));
return tempVector;
}
解密代码(非常简单):
string StaticFunctions::decrypt(string decryptThis)
{
for(int x = 0; x < decryptThis.length(); x++)
{
switch(decryptThis[x])
{
case '*':
{
decryptThis[x] = '0';
break;
}
case '?':
{
decryptThis[x] = '1';
break;
}
case '!':
{
decryptThis[x] = '2';
break;
}
case '=':
{
decryptThis[x] = '3';
break;
}
case '#':
{
decryptThis[x] = '4';
break;
}
case '^':
{
decryptThis[x] = '5';
break;
}
case '%':
{
decryptThis[x] = '6';
break;
}
case '+':
{
decryptThis[x] = '7';
break;
}
case '-':
{
decryptThis[x] = '8';
break;
}
case '"':
{
decryptThis[x] = '9';
break;
}
}
}
return decryptThis;
}
最佳答案
尝试改用strtol
strtol(splitString [0] .c_str(),NULL,10);