计算文件中的整数并复制到动态数组

时间:2014-10-31 12:32:07

标签: c++ arrays file

我正在使用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。所以我有点困惑我做错了。

2 个答案:

答案 0 :(得分:0)

std::vector及其push_back功能的组合将起到作用。无需预先计算迷宫的大小。

由于您似乎不熟悉std::vector,我强烈建议您自己进行锻炼。但是,我将here一个(很多)广泛使用STL的解决方案,包括std::stringstreamstd::copystd::back_inserterstd::getline。我还展示了如何获得行数和列数。请注意,我还使用了像for-range和auto这样的C ++ 11功能。

答案 1 :(得分:-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;
}