我正在爲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
感謝您抽出時間來閱讀我的冗長的問題:)
回覆:*「一個額外的問題」* - 單獨的問題屬於單獨的帖子。 – 2015-03-02 19:23:18
我不認爲它是一個額外的問題,因爲它們相當連接:)對不起:) – Patidati 2015-03-02 19:25:22