我正在C++中制作平铺引擎/游戏。我所苦恼的是,我无法弄清楚如何从文件加载和处理整数数据。无法正确读取文件中的整数C++
这里是mapfile.map:
[[email protected] ~]$ cat cpp/adventures_of_ironville/mapfile.map
0000001111101111
0000111000020000
1100000000000000
0100200000111000
0110000000111200
0010002200111120
2010002220111111
0010022200001111
0000001111101111
0000111000020000
1100000000000000
0100200000111000
0110000000111200
0010002200111120
2010002220111111
0010022200001111
[[email protected] ~]$
这是要在我的比赛水平。现在,对于代码,我挣扎:
bool World::loadLevelFromDisk(std::string pathToMapFile)
{
//Our file object
std::ifstream file(pathToMapFile);
//Store contents of file
char tempData;
if (!file.is_open()) {
//Error...
return false;
} else { //File is ready to use
while (!file.eof())
{
//Grab one character.
tempData = file.get();
//Convert that character
//to ascii integer.
int data = tempData - 48;
//std::vector used elsewhere,
//which must take an int.
currentLevel.push_back(data);
//Why does this output a bunch of -38's?
//expected output is to be identical to the
//mapfile.map.
std::cout << data;
}
}
}
去年性病的输出::法院:
0000001111101111-380000111000020000-381100000000000000-380100200000111000-380110000000111200-380010002200111120-382010002220111111-380010022200001111-380000001111101111-380000111000020000-381100000000000000-380100200000111000-380110000000111200-380010002200111120-382010002220111111-380010022200001111-38-49
我怎样才能从文件中读取整型数据?我花了很多时间尝试不同的解决方案,从串流到boost :: lexical_cast,我还没有找到可行的答案。这是我得到的最接近预期结果的文件,它将整个文件读取为可以在其他地方操作。所有帮助非常感谢!
备注:不要使用'file.eof'。用'file.good()'检查它,或者使用内置的运算符if(file)'。 – Blacktempel
我可以问一下不使用file.eof()的原因吗? – JohnBobSmith
请参阅stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong查看如何以及为什么要从文件中正确读取。 – iksemyonov