C ++偷看赋值'ÿ'(ifstream)

时间:2016-03-26 16:12:16

标签: c++ file filestream peek

我的代码首先是:

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

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

1 个答案:

答案 0 :(得分:1)

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

要解决此问题,您需要从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. 您返回的highScoreLvl1值可能会保持未初始化状态,因为字符串中不能包含“#”。也许你的意思是返回highScoreLvl1,highScoreLvl2或highScoreLvl3的最大值。
  2. 您将char的值转换为int。在这种情况下,您将无法获得1,2等的值。您将获得ASCII字符的序数,例如0x31(49)表示“1”,0x32(50)表示2,等等。如果需要数字值,可以执行以下操作:highScoreLvl1 = textInFile[i + 2] - '0';