2014-10-31 54 views
-2

我目前正在使用a-star算法編程一個程序。因此,我生成一個隨機迷宮並將其保存在.txt文件中。該文件看起來有點像這樣:計算文件中的整數並複製到動態數組

19999999199999991 
19191119111919191 

其中1是一堵牆,和9是一片空白。

現在我必須將該文件讀入findpath程序,該程序將該文件讀取到數組中。然後程序計算最短路徑。


當我只是將文件的整數複製到源代碼一切工作正常。但是現在我想讓這個程序更加動態化,因此;我想讀取文件,計算所需的數組大小,並將整數存儲在數組中,一次完成。現在

我的大問題是,我不知道如何在文件中讀取並獲得迷宮的大小。

對於我的功能,我將不得不計算行數和列數的文件中,生成的陣列和整數存儲在陣列中,但我沒有線索如何做到這一點。我的一個問題是整數不能被空格分開,而且我不能更改生成文件的程序。

我已經知道如何打開該文件,但;

  • 如何獲取文件的大小(一行中的整數數量和行數)以及;
  • 如何將整數分別存儲在數組中?

編輯:

所以我更新我的程序用下面的代碼:

main 
{ 
    ifstream myfile("BLOCK_style_maze.txt"); 
    string line; 
    int colCount=0; 
    int rowCount=0; 
    int temp=0; 

    if(myfile.is_open()) 
    { 
     if(getline(myfile,line)) 
     { 
      rowCount++; 
      int i=0; 
      for(i=0;i<line.length();i++) 
      { 
       if(line.at(i)=='1' || line.at(i)=='9') colCount++; 
      } 
     } 
     while(getline(myfile, line)) 
     { 
      rowCount++; 
     } 
     cout << "R:"<< rowCount << "C:" << colCount << endl; 
     myfile.close(); 
    } 
    else 
    { 
     cout << "Unabale to open maze file"; 
    } 

    MAP_WIDTH = colCount; 
    MAP_HEIGHT = rowCount; 

    map=new int [MAP_WIDTH*MAP_HEIGHT]; 
    int k=MAP_WIDTH*MAP_HEIGHT; 
    int j=0; 

    if (myfile.is_open()) 
    { 
     while(myfile >> temp) 
     { 
      map[j++] = temp; 
     } 
    } 

    for(int i=0; i<=k; i++) 
    { 
     cout << map[i]<< endl; 
    } 
} 

爲了測試我想打印在控制檯上的矩陣地圖的入口代碼,但我只是得到0作爲輸出。所以我有點困惑我做錯了什麼。

+1

的std ::矢量你炒。你不需要告訴他任何東西的大小,它只是....作品:D – SlySherZ 2014-10-31 12:35:29

回答

0

std::vector一個combinaison及其push_back功能會做的伎倆。不需要預先計算迷宮的大小。

因爲你似乎不熟悉std::vector我強烈建議你自己做練習。但是,我把here一個(很多)廣泛使用STL的解決方案,包括std::stringstream,std::copy,std::back_inserterstd::getline。我還展示瞭如何獲取行和列的數量。請注意,我也使用C++ 11功能,如for-range和auto

+0

我的問題是,我需要MAP_Width和MAP_Height爲我的未來計算。因此我需要計算這兩個變量。你能告訴我數組和std :: vector之間的diverenc嗎?我正在使用一些代碼來解決迷宮問題,並將其適應於我的需求。我沒有真正允許在代碼中進行很多改變。所以這就是爲什麼我特意要求一個數組。 – user3794592 2014-10-31 12:42:12

+2

@ user3794592:在閱讀地圖文件之前,您不需要知道寬度和高度,是嗎?所以你可以在閱讀文件後獲取矢量的大小。 – 2014-10-31 12:49:46

+0

@ user3794592,Christian說得很好:使用'std :: vector :: size'成員函數來獲取行數和列數。 – Hiura 2014-10-31 12:59:21

-1

這並不是一個非常艱鉅的任務,你可以試試下面的代碼:

#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

int main() { 
    string line; 
    ifstream myfile ("example.txt"); 
    int colCount = 0; 
    int rowCount = 0; 
    if (myfile.is_open()) 
    { 
    if (getline(myfile,line)) //Read the first line to get the number of columns 
    { 
     rowCount++; //make sure we count the first line as a row 
     int i = 0; 
     for (i=0;i<line.length();i++) 
     { 
     if (line.at(i) == '1' or line.at(i) == '9') colCount++; //each 1 or 9 means a column, we want to ignore other characters like '\n' or spaces 
     } 
    } 
    while (getline(myfile,line)) //Read the rest of the lines to get the rest of the rows. 
    { 
     rowCount++; 
    } 
    myfile.close(); 
    } 
    cout << "rows=" << rowCount << '\n'; 
    cout << "cols=" << colCount << '\n'; 
    // now that we've counted, let's define our arrays and reopen the file. 
    char** map = new char*[rowCount]; 
    for(int i = 0; i < rowCount; ++i) 
    { 
    map[i] = new char[colCount]; 
    } 

    int currentRow = 0; 
    ifstream myfile2 ("example.txt"); 
    if (myfile2.is_open()) 
    { 
    while(getline(myfile2,line)) 
    { 
     for (int i=0;i<colCount;i++) 
     { 
     map[currentRow][i] = line.at(i); 
     } 
     currentRow++; 
    } 
    } 

    // you can now access this array as a 2d array. point = map[row][column] 
    for(int i=0;i<colCount;i++){ 
    cout << map[0][i]; //Print the first row! 
    } 
    cout << '\n'; 

    return 0; 
} 
+0

你在哪裏存儲迷宮信息?不要介意行數和列數 - 你需要把所有的1和9放在某個地方,一旦你做完了,確定迷宮的大小是微不足道的。 – 2014-10-31 12:58:56

+0

我完成之前點擊保存。我在分配2D陣列後第二次打開文件。我明白,向量顯然是做到這一點的「正確」方式,但動態分配數組對於低級編程是一個非常重要的概念,它也直接回答他的問題,而不是標準的「你做錯了」回答。 – Optox 2014-10-31 13:03:16

+0

你的代碼不能編譯,最重要的是它會泄漏。 – Hiura 2014-10-31 16:36:12