2015-03-02 41 views
0

我正在爲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

回答

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)) 
+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

0

在你的第一個功能提供,這條線

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