【问题标题】:Count integers in file and copy to dynamic array计算文件中的整数并复制到动态数组
【发布时间】:2014-10-31 12:32:06
【问题描述】:

我目前正在使用 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 作为输出。所以我有点困惑我做错了什么。

【问题讨论】:

  • std::vector 是你炸的。你不需要告诉他任何东西的大小,它只是......有效:D

标签: c++ arrays file


【解决方案1】:

std::vector 及其push_back 函数的组合可以解决问题。无需预先计算迷宫的大小。

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

【讨论】:

  • 我的问题是我需要 MAP_Width 和 MAP_Height 来进行未来的计算。因此我需要计算这两个变量。你能告诉我数组和 std::vector 之间的区别吗?我正在使用一些代码来解决迷宫并使其适应我的需要。我真的不允许在代码中进行很多更改。所以这就是我特意要一个数组的原因。
  • @user3794592:在阅读地图文件之前,您不需要知道宽度和高度,是吗?因此,您可以在读取文件后获取向量的大小。
  • @user3794592,Christian说的很好:使用std::vector::size成员函数获取行数和列数。
  • @user3794592 我已更新答案以添加更多提示和可能的解决方案。 hth
  • 在我看来,这是他不能使用 STL 的家庭作业。
【解决方案2】:

这不是一个很难的任务,试试下面的代码:

#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;
}

【讨论】:

  • 迷宫信息存储在哪里?别管行数和列数——你需要把所有的 1 和 9 放在某个地方,一旦你这样做了,确定迷宫的大小就很简单了。
  • 我在完成之前点击了保存。分配二维数组后,我第二次打开文件。我知道向量显然是做到这一点的“正确”方式,但是数组的动态分配对于低级编程来说是一个非常重要的概念,它也直接回答了他的问题,而不是标准的“你做错了”回答。
  • 您的代码无法编译,最重要的是它会泄漏。
  • 我承认我手头没有编译器,但它仍然可以作为示例。程序结束时释放内存。当您丢失指向内存块的指针时,就会发生内存泄漏。
  • 修复了它,我检查了它现在可以编译并在一个示例上运行。
猜你喜欢
  • 1970-01-01
  • 2016-04-17
  • 2018-07-22
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多