首先是我的代码:
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;
}
它检测到“#”,然后在执行 peek 操作时c
被赋值。'ÿ'
它应该给出的是数字'1'
,'2'
或'3'
(以字符形式);但它不是出于某种原因,我不明白为什么......:/
文件如下所示:
level#12level#22level#32
第一个数字代表级别,第二个数字是在该级别上取得的分数。