当在C++中进行文件分析时跳到特定的行值

问题描述:

我正在为Conway的生命游戏编写.lif 105的文件分析器。 它工作正常,除了我想跳过上面的#N文本段,它标记了评论的结尾。但是由于某种原因,这只会跳过前三行。当在C++中进行文件分析时跳到特定的行值

下面是一个例子:

#Life 1.05 
#D Name: 1 beacon 
#D Approximately the 32nd-most common oscillator. 
#D www.conwaylife.com/wiki/index.php?title=1_beacon 
#N 
#P 10 10 
..** 
.*.* 
*..*.** 
**.*..* 
.*.* 
.*..* 
..** 

它将跳过行:

#Life 1.05 
#D Name: 1 beacon 
#D Approximately the 32nd-most common oscillator. 

在该小区段所得开始3 Y-帘线低于它应该。

#include "fileparser.h" 
#include <QCoreApplication> 
#include <fstream> 
#include <string> 
#include <sstream> 

using namespace std; 
string filetype = "#Life 1.05"; 

void getGame(bool array[100][100], const string& fname){ 

    // create some objects in memory 

    ifstream infile("C:/GameOfLife/ellisonp4hwemulator_105.lif"); 

    string testType, line; 
    int xPos= 0, yPos= 0, temp; 

    // read objects 

    bool comments = true; 
    while (std::getline(infile, line) && comments) 
    { 
     if(line.find("#N") == std::string::npos) 
      comments = false; 
    } 

    std::getline(infile, line); 
    std::istringstream iss(line); 



    while(std::getline(infile, line)){ 
     std::istringstream iss(line); 
     iss >> line; 
     temp = xPos; 
     for(char c : line){ 
      if(c == '*') 
       array[temp][yPos] = true; 
      temp++; 
     } 
     yPos++; 
    } 

    infile.close(); // optional 

} 

对于那些愿意帮助更多的人来说还有一个额外的问题!最初我想让#P来标记单元格的开始部分。所以在这种情况下,它将开始在X-10 Y-10上绘图。但是找不到它。以下是一个代码,如果你想帮助一些额外的:)

#include "fileparser.h" 
#include <QCoreApplication> 
#include <fstream> 
#include <string> 
#include <sstream> 

using namespace std; 
string filetype = "#Life 1.05"; 

void getGame(bool array[100][100], const string& fname){ 

    // create some objects in memory 

    ifstream infile("C:/GameOfLife/ellisonp4hwemulator_105.lif"); 

    string testType, line, emptyValue; 
    int xPos, yPos, temp; 

    // read objects 

    bool comments = true; 
    while (std::getline(infile, line) && comments) 
    { 
     if(line.find("#N") == std::string::npos) 
      comments = false; 
    } 

    std::getline(infile, line); 
    std::istringstream iss(line); 
    iss >> emptyValue >> xPos >> yPos; 


    while(std::getline(infile, line)){ 
     std::istringstream iss(line); 
     iss >> line; 
     temp = xPos; 
     for(char c : line){ 
      if(c == '*') 
       array[temp][yPos] = true; 
      temp++; 
     } 
     yPos++; 
    } 

    infile.close(); // optional 

} 

我想ISS >> emptyValue >> XPOS >> yPos;

搭上值emptyValue =#P,XPOS = 10,yPos = 10

感谢您抽出时间来阅读我的冗长的问题:)

+0

回复:*“一个额外的问题”* - 单独的问题属于单独的帖子。 – 2015-03-02 19:23:18

+0

我不认为它是一个额外的问题,因为它们相当连接:)对不起:) – Patidati 2015-03-02 19:25:22

if(line.find("#N") == std::string::npos) 
    comments = false; 

这是什么的对面你要。如果"#N"找不到,则条件为真。这将是该文件的第一行,所以这是所有的循环读取。简单地操作切换到!=

if(line.find("#N") != std::string::npos) 
    comments = false; 

实际上,循环将读取下一行过了,我不认为这是你想要的。您可以修复,通过从该切换条件检查的顺序while循环:

while (std::getline(infile, line) && comments) 

这样:

while (comments && std::getline(infile, line)) 
+0

呵呵谢谢!不知道为什么它== :)! 现在的问题是,它跳过太多的行,解决了它! :D谢谢:) – Patidati 2015-03-02 19:17:00

+0

@Patidati:刷新页面,我添加了更多信息。 – 2015-03-02 19:19:39

+0

感谢:D!它现在修复:) – Patidati 2015-03-02 19:26:09

在你的第一个功能提供,这条线

if(line.find("#N") == std::string::npos) 

应该是

if(!(line.find("#N") == std::string::npos)) 

你要这个段被发现后,将注释设置为false,而不是如果没有找到

编辑:

在while循环的条件需要交换前后过。我刚刚发现了这一点,不幸在本杰明林德利之后 - 看到他的回答详情

+0

谢谢:)!愚蠢的错误:)! – Patidati 2015-03-02 19:17:56

+0

没问题,我无法让xPos和yPos工作,但我确信这是正确的方法。 – Kvothe 2015-03-02 19:22:09

+0

我没有解决它;)只是一些小错误:) – Patidati 2015-03-02 19:25:48