如何从.txt文件中读取两个2d数组?

时间:2014-12-29 01:12:36

标签: c++

所以我有一个任务是从.txt文件中读取两个2d数组。该文件如下所示:

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

'#' character用于定义两个数组之间的边界。

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

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;

现在,接下来我应该做些什么来识别&#39;#&#39;字符并继续阅读下一个矩阵?下一个2d数组应该被称为ptr_two

请注意,我不能使用矢量,只能使用2d数组。

1 个答案:

答案 0 :(得分:0)

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

我的修改基本上允许您拉入任意数量的表。但是,它不处理锯齿状表格。它也无法正确处理文件以&#39;#&#39;结尾的边缘情况。字符(空表)。最后有一种情况,如果你没有在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);