2014-10-31 53 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