0

我正在为康威的生命游戏编写 .lif 105 的文件解析器。它工作正常,除了我想跳过 #N 上方的文本段,它标志着评论的结束。但这只是出于某种原因跳过了前三行。

这是一个例子:

#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

感谢您花时间阅读我冗长的问题:)

4

2 回答 2

1
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))
于 2015-03-02T19:14:11.477 回答
0

在您提供的第一个功能中,这一行

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

应该

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

您希望它在找到此段后将评论设置为 false,而不是在未找到时

编辑:

while 循环中的条件也需要交换。不幸的是,在本杰明·林德利之后,我才发现了这一点-有关详细信息,请参阅他的答案

于 2015-03-02T19:14:48.040 回答