我写了一个非常简单的函数,它读入可能的玩家名称并将它们存储在地图中供以后使用。基本上在文件中,每一行都是一个新的可能的玩家名字,但由于某种原因,它似乎除了姓氏之外都有一些看不见的新行字符。我的打印输出显示它是这样的...文本文档中的换行符?
nameLine = Georgio
Name: Georgio
0
nameLine = TestPlayer
Name: TestPlayer 0
这里是实际的代码。我认为我需要剥离某些东西,但我不确定我需要检查什么。
bool PlayerManager::ParsePlayerNames()
{
FileHandle_t file;
file = filesystem->Open("names.txt", "r", "MOD");
if(file)
{
int size = filesystem->Size(file);
char *line = new char[size + 1];
while(!filesystem->EndOfFile(file))
{
char *nameLine = filesystem->ReadLine(line, size, file);
if(strcmp(nameLine, "") != 0)
{
Msg("nameLine = %s\n", nameLine);
g_PlayerNames.insert(std::pair<char*, int>(nameLine, 0));
}
for(std::map<char*,int>::iterator it = g_PlayerNames.begin(); it != g_PlayerNames.end(); ++it)
{
Msg("Name: %s %d\n", it->first, it->second);
}
}
return true;
}
Msg("[PlayerManager] Failed to find the Player Names File (names.txt)\n");
filesystem->Close(file);
return false;
}
在地图中使用'char *'作为键时要小心。而是使用'std :: string'来表示字符串。 – 2010-11-10 10:27:44
names.txt文件如何显示(它给出了上述结果)? – Default 2010-11-10 10:30:47
你确定ReadLine不读取换行符吗? – Default 2010-11-10 10:33:08