C++ peek 赋值 'ÿ' (ifstream)

C++ peek giving value 'ÿ' (ifstream)

我的代码首先是:

int GetHighScore(string name)
{
    int highScore = 0;
    ifstream fin;
    char textInFile[50];1

    fin.open(name + ".txt", ios::in);

    if (fin.fail())
    {
        // Old piece of code
        highScore = 0;
    }
    else
    {
        while (fin.good())
        {
            fin >> textInFile;
            for each (char var in textInFile)
            {
                if (var == '#')
                {
                    char c = fin.peek();

                    if (c == '1')
                    {
                        char score = fin.peek();
                        highScoreLvl1 = (int)score;
                    }
                    else if (c == '2')
                    {
                        char score = fin.peek();
                        highScoreLvl2 = (int)score;
                    }
                    else if (c == '3')
                    {
                        char score = fin.peek();
                        highScoreLvl3 = (int)score;
                    }
                }
            }
        }
        //fin >> highScore;
    }


    // Return the high score found in the file
    return highScoreLvl1;
}

它检测到“#”,但随后在执行查看操作时 c 被分配了值 'ÿ'。它应该给出的是数字'1''2''3'(char形式);但出于某种原因它不是,我不明白为什么......:/

文件如下所示:

level#12level#22level#32

第一个数字代表级别,第二个数字是该级别的分数。

如果您的文件包含唯一的字符串 'level#12level#22level#32',那么它将在 fin >> textInFile 运算符中读入 textInFile。当您在字符串中遇到“#”字符时,您正试图从文件流中查看字符,但没有什么可查看的,这就是为什么 -1(文件结尾)是 returned.

的原因

要解决此问题,您需要从 textInFile 字符串中获取下一个字符,而不是从文件中获取。这是示例代码:

int GetHighScore(string name)
{
    int highScore = 0;
    ifstream fin;
    char textInFile[50];

    fin.open(name + ".txt", ios::in);

    int highScoreLvl1, highScoreLvl2, highScoreLvl3;

    if (fin.fail())
    {
        // Old piece of code
        highScore = 0;
    }
    else
    {
        while (fin.good())
        {
            fin >> textInFile;
            bool bPrevIsHash = false;
            size_t nLength = strlen(textInFile);
            for (size_t i = 0; i + 2 < nLength; ++i)
            {
                if (textInFile[i] == '#')
                {
                    if (textInFile[i + 1] == '1')
                    {
                        highScoreLvl1 = (int)textInFile[i + 2];
                    }
                    else if (textInFile[i + 1] == '2')
                    {
                        highScoreLvl2 = (int)textInFile[i + 2];
                    }
                    else if (textInFile[i + 1] == '3')
                    {
                        highScoreLvl3 = (int)textInFile[i + 2];
                    }
                }
            }
        }
    }


    // Return the high score found in the file
    return highScoreLvl1;
}

您的代码还有其他几个问题:

  1. 您 return highScoreLvl1 的值可能未初始化,因为字符串中不能有“#”。并且您的意思可能是 return highScoreLvl1、highScoreLvl2 或 highScoreLvl3 的最大值。
  2. 您正在分配转换为 int 的 char 值。在这种情况下,您将不会获得 1、2 等的值。您将获得 ASCII 字符的序号,例如0x31 (49) 表示“1”,0x32 (50) 表示 2,等等。如果您需要数字值,您可以执行以下技巧:highScoreLvl1 = textInFile[i + 2] - '0';