0

アップデート

私は 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 を試しましたが、役に立ちませんでした。

ありがとう、

  • マーカス

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

2 に答える 2

0

atoi(string.c_str()) の代わりに stoi(string) で解決しました。

更新:解決しませんでした。

于 2012-09-26T08:29:26.207 に答える
0

代わりに strtol を使用してみてください

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

于 2012-09-26T08:12:52.847 に答える