【问题标题】:How to read two 2d arrays from a .txt file?如何从 .t​​xt 文件中读取两个二维数组?
【发布时间】:2014-12-29 01:12:35
【问题描述】:

所以我的任务是从 .txt 文件中读取两个二维数组。该文件如下所示:

2 3 4 5 6 7
2 6 8 2 4 4
6 3 3 44 5 1
#
4 2 1 6 8 8
5 3 3 7 9 6
0 7 5 5 4 1

“#”字符用于定义两个数组之间的边界。

两个二维数组都必须动态分配。这就是我所拥有的:

int col = 0, row = 1;
int temp = 0;
char c;
ifstream file("input1.txt");
col = 0;
row = 1;
// calculating the no. of rows and columns of first 2d array in the text file (don't know how to do that for the second but this works perfectly)
do {
    file.get(c);
    if ((temp != 2) && (c == ' ' || c == '\n'))
        col++;
    if (c == '\n')
    {
        temp = 2;
        row++;
    }
} while (file);
// Used for rewinding file.
file.clear();
file.seekg(0, ios::beg);
// now I am using the info on rows and columns to dynamically allocate 2d array.(this is working fine too)
int** ptr = new int*[row];
for (int i = 0; i < col; i++)
{
    ptr[i] = new int[col];
}
// here I am filling up the first 2d array.
for (int i = 0; i < row; i++)
{
    for (int k = 0; k < col; k++)
    {
        file >> ptr[i][k];
        cout << ptr[i][k] << ' ';
    }
    cout << endl;
}
// code above works fine when there is only one matrix in file(without the '#' sign)
// these are used for storing dimensions of second 2d array and for dynamically allocating him
int row_two = 1, col_two = 0;

现在,为了识别“#”字符并继续阅读下一个矩阵,我接下来应该做什么?下一个二维数组应该被称为ptr_two

请注意,我不能使用向量,只能使用二维数组。

【问题讨论】:

  • 什么是fajl?此外,您应该找到一种无需两次读取文件的方法。考虑使用 'getline()' 和 stringstream
  • 您可以使用动态存储容器,每次读取时添加一个字符。无需重读文件
  • 请修改您的问题。
  • @DwayneTowell 'fajl' 是我的语言中的文件名称。现在修好了。我想到了这一点,如果我能找到一种只读取整个文件一次的方法,那就太好了,但我不知道该怎么做。而且我真的不知道怎么用stringstream我还在c++的开始..
  • @dwn 我该怎么做?什么是储存容器?

标签: c++


【解决方案1】:

这比您想要的要更进一步,但基本概念就在那里。简而言之,使用 file.tellg() 函数来保存您当前的位置。您可以稍后使用它返回您的位置。

我的修改基本上允许您拉入任意数量的表格。但是,它不处理锯齿状表。它也不能正确处理文件以“#”字符结尾的边缘情况(空表)。最后有一种情况,如果你不在 input1.txt 的末尾添加换行符,最后一行就不会被拉取。

无论如何,这段代码是在没有使用调试器进行仔细检查的情况下修改的,所以我不保证它会在没有任何工作的情况下启动。或者当它工作时你不会得到一个无限循环。

// note that row starts from 0 because there is going to be an endline before the '#' character when it wasn't there before.
int col = 0, row = 0;
int temp = 0;
// new variables.
int table = 1, filepos = 0, curtable = 0;
char c;
ifstream file("input1.txt");
col = 0;
row = 1;

// calculating the number of tables.
do {
    file.get(c);
    if (c == '#')
        table++;
} while (file);

// Used for rewinding file.
file.clear();
file.seekg(0, ios::beg);

int*** ptrtable = new int**[table];

// calculating the no. of rows and columns of first 2d array in the text file (don't know how to do that for the second but this works perfectly)
do {
    file.get(c);
    if (c == ' ' || c == '\n')
        col++;
    if (c == '\n')
    {
        temp = 2;
        row++;
    }
    if (c == '#')
    {
        // Used for rewinding file.
        file.clear();
        temp = file.tellg;
        file.seekg(filepos, ios::beg);
        filepos = temp;

         // now I am using the info on rows and columns to dynamically allocate 2d array.(this is           working fine too)
         int** ptr = new int*[row];
         for (int i = 0; i < col; i++)
         {
             ptr[i] = new int[col];
         }
         // here I am filling up the first 2d array.
         for (int i = 0; i < row; i++)
         {
             for (int k = 0; k < col; k++)
             {
                 file >> ptr[i][k];
                 cout << ptr[i][k] << ' ';
             }
             cout << endl;
         }

         ptrtable[curtable] = ptr;

         // prepare for the next table.
         curtable++;
         temp = 0;
         row = -1;
         col = 0;

         file.clear();
         // filepos + 1 to jump past the '#' character.
         file.seekg(filepos + 1, ios::beg);

    }

} while (file);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 2014-03-17
    • 1970-01-01
    相关资源
    最近更新 更多