2016-07-08 45 views
1

我正在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,我还没有找到可行的答案。这是我得到的最接近预期结果的文件,它将整个文件读取为可以在其他地方操作。所有帮助非常感谢!

+0

备注:不要使用'file.eof'。用'file.good()'检查它,或者使用内置的运算符if(file)'。 – Blacktempel

+0

我可以问一下不使用file.eof()的原因吗? – JohnBobSmith

+3

请参阅stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong查看如何以及为什么要从文件中正确读取。 – iksemyonov

回答

3

的-38实际上是line feed\n字符) 你读10(用于换行符ASCII码,然后计算10 - 48)

只是这种修改在你的代码中省略掉:

... 
tempData = file.get(); 
if (tempData == '\n' || tempData =='\r') continue; 
... 

\r字符只适用于窗户,但不伤害要格外小心。

另一种方法是简单地忽略任何东西,这不是一个数字,在这种情况下,条件是:

if (tempData < '0' || tempData > '9') continue; 
+0

哇!非常感谢你!改变eof循环条件删除多余的-49,并做这里建议的问题解决了我的问题! – JohnBobSmith

1

我注意到了有关你的代码的第一件事是你使用!file.eof()这几乎总是循环坏事:

参见:比,你是不是只检查来选择你要转换的字符代码Why is iostream::eof inside a loop condition considered wrong?

其他(如在其他答案中提到)。

std::vector<char> currentLevel; 

bool loadLevelFromDisk(std::string pathToMapFile) 
{ 
    //Our file object 
    std::ifstream file(pathToMapFile); 

    if(!file.is_open()) 
    { 
     //Error... 
     return false; 
    } 

    //File is ready to use 

    //Store contents of file 
    char tempData; 

    // grab one char 
    while(file.get(tempData)) // never loop on eof() 
    { 
     // you only get here if file.get() was a success 

     if(tempData < '0' || tempData > '9') 
      continue; // ignore irrelevant characters 

     //Convert that character 
     //to ascii integer. 
     int data = tempData - '0'; // more portable 

     //std::vector used elsewhere, 
     //which must take an int. 
     currentLevel.push_back(data); 

     std::cout << data; 
    } 

    return true; // don't forget to indicate success 
} 

如果函数成功,您忘了返回true。如果增加编译器的警告级别(我使用-Wall -Wextra -pedantic-errorsGCC),则会检测到此错误。