【问题标题】:Occasional unhandled exception with atoi(string.c_str()) Updatedatoi(string.c_str()) 偶尔出现未处理的异常已更新
【发布时间】:2012-09-26 08:02:20
【问题描述】:

更新

我认为 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' 分割字符串。我调试的时候,savefile总是一样的,第一个值是3。

这项工作有时可能每 10 次尝试一次。因此,每 10 次尝试中的 9 次,我都会在内存位置的...处得到未处理的异常。

监控转换后的值显示它总是返回 3,然后应用程序不会崩溃,直到我开始游戏,这在代码中有点远。

如果我删除 atoi 并返回 3,则应用程序可以正常工作。

我试过 strtod 但没有帮助。

谢谢,

  • 马库斯

拆分字符串代码:

 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;
}

【问题讨论】:

  • 不是说和你的问题有什么关系,但是不需要用string line = "";初始化line;只是string line; 将创建一个空字符串。
  • 你能发布StaticFunctions::splitString()的定义吗?
  • StaticFunctions::splitStringStaticFunctions::decrypt 是如何实现的?也许他们弄乱了您稍后使用atoi 处理的字符串?
  • 试试 std::stoi(C++11,使用 std::string 而不是 const char*)。如果你弄乱了字符串,它会抛出invalid argument(如果字符串不能转换为整数)或out_of_range(如果字符串表示的整数太大)。至少你会知道哪里出了问题。
  • 感谢所有建议,使用带有字符串的 stoi 而不是 atoi 和 const *char 解决了它。

标签: c++ c exception exception-handling atoi


【解决方案1】:

尝试使用 strtol

strtol(splitString[0].c_str(),NULL,10);

【讨论】:

    【解决方案2】:

    stoi(string) 而不是 atoi(string.c_str()) 解决了它。

    更新: 没解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 2015-01-17
      • 1970-01-01
      相关资源
      最近更新 更多