如何在不知道C ++长度的情况下从文件中读取2d数组?

时间:2023-01-31 21:29:35

Like the title says I'm trying to read an unknown number of integers from a file and place them in a 2d array.

就像标题所说我试图从文件中读取未知数量的整数并将它们放在二维数组中。

#include <iostream>
#include <fstream>
using namespace std;
int main()
{

fstream f;int i,j,n,a[20][20];char ch;

i=0;j=0;n=0;
f.open("array.txt", ios::in);
while(!f.eof())
{
    i++;
    n++;
    do
    {
        f>>a[i][j];
        j++;
        f>>ch;
    }
    while(ch!='\n');
}

for(i=1;i<=n;i++)
{
    for(j=1;j<=n;j++)
        cout<<a[i][j]<<endl;
    cout<<endl;
}
return 0;

}

and my "array.txt" file :

和我的“array.txt”文件:

1 1 1
2 2 2
3 3 3

After compiling the program, it prints this

编译完程序后,会打印出来

如何在不知道C ++长度的情况下从文件中读取2d数组?

3 个解决方案

#1


4  

As your input file is line oriented, you should use getline (C++ equivalent or C fgets) to read a line, then an istringstream to parse the line into integers. And as you do not know a priori the size, you should use vectors, and consistently control that all lines have same size, and that the number of lines is the same as the number of columns.

由于您的输入文件是面向行的,您应该使用getline(C ++等效或C fgets)来读取一行,然后使用istringstream将该行解析为整数。由于您不知道大小的先验,您应该使用向量,并始终控制所有行具有相同的大小,并且行数与列数相同。

Last but not least, you should test eof immediately after a read and not on beginning of loop.

最后但并非最不重要的一点是,您应该在读取后立即测试eof,而不是在循环开始时测试。

Code becomes:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main()
{

    fstream f;
    int i=0, j=0, n=0;
    string line;
    vector<vector<int>> a;
    f.open("array.txt", ios::in);
    for(;;)
    {
        std::getline(f, line);
        if (! f) break; // test eof after read
        a.push_back(vector<int>());
        std::istringstream fline(line);
        j = 0;
        for(;;) {
            int val;
            fline >> val;
            if (!fline) break;
            a[i].push_back(val);
            j++;
        }
        i++;
        if (n == 0) n = j;
        else if (n != j) {
            cerr << "Error line " << i << " - " << j << " values instead of " << n << endl;
        }
    }
    if (i != n) {
        cerr << "Error " << i << " lines instead of " << n << endl;
    }

    for(vector<vector<int>>::const_iterator it = a.begin(); it != a.end(); it++) {
        for (vector<int>::const_iterator jt = it->begin(); jt != it->end(); jt++) {
            cout << " " << *jt;
        }
        cout << endl;
    }
    return 0;
}

#2


1  

You may want to look into using a vector so you can have a dynamic array.

您可能希望研究使用向量,以便拥有动态数组。

#3


0  

Try:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main() {
  fstream f;
  int i, j, n, a[20][20];
  string buf;

  i = 0;
  j = 0;
  n = 0;
  f.open("array.txt", ios::in);
  while (1) {
    getline(f, buf);
    if (f.eof()) break;
    stringstream buf_stream(buf);
    j = 0;
    do {
      buf_stream >> a[i][j];
      j++;
    } while (!buf_stream.eof());
    i++;
    n++;
  }

  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) cout << a[i][j] << " ";
    cout << endl;
  }
  return 0;
}

Also, if you really want to read arbitrarily large arrays, then you should use std::vector or some such other container, not raw arrays.

另外,如果你真的想要读取任意大的数组,那么你应该使用std :: vector或其他一些容器,而不是原始数组。

#1


4  

As your input file is line oriented, you should use getline (C++ equivalent or C fgets) to read a line, then an istringstream to parse the line into integers. And as you do not know a priori the size, you should use vectors, and consistently control that all lines have same size, and that the number of lines is the same as the number of columns.

由于您的输入文件是面向行的,您应该使用getline(C ++等效或C fgets)来读取一行,然后使用istringstream将该行解析为整数。由于您不知道大小的先验,您应该使用向量,并始终控制所有行具有相同的大小,并且行数与列数相同。

Last but not least, you should test eof immediately after a read and not on beginning of loop.

最后但并非最不重要的一点是,您应该在读取后立即测试eof,而不是在循环开始时测试。

Code becomes:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main()
{

    fstream f;
    int i=0, j=0, n=0;
    string line;
    vector<vector<int>> a;
    f.open("array.txt", ios::in);
    for(;;)
    {
        std::getline(f, line);
        if (! f) break; // test eof after read
        a.push_back(vector<int>());
        std::istringstream fline(line);
        j = 0;
        for(;;) {
            int val;
            fline >> val;
            if (!fline) break;
            a[i].push_back(val);
            j++;
        }
        i++;
        if (n == 0) n = j;
        else if (n != j) {
            cerr << "Error line " << i << " - " << j << " values instead of " << n << endl;
        }
    }
    if (i != n) {
        cerr << "Error " << i << " lines instead of " << n << endl;
    }

    for(vector<vector<int>>::const_iterator it = a.begin(); it != a.end(); it++) {
        for (vector<int>::const_iterator jt = it->begin(); jt != it->end(); jt++) {
            cout << " " << *jt;
        }
        cout << endl;
    }
    return 0;
}

#2


1  

You may want to look into using a vector so you can have a dynamic array.

您可能希望研究使用向量,以便拥有动态数组。

#3


0  

Try:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main() {
  fstream f;
  int i, j, n, a[20][20];
  string buf;

  i = 0;
  j = 0;
  n = 0;
  f.open("array.txt", ios::in);
  while (1) {
    getline(f, buf);
    if (f.eof()) break;
    stringstream buf_stream(buf);
    j = 0;
    do {
      buf_stream >> a[i][j];
      j++;
    } while (!buf_stream.eof());
    i++;
    n++;
  }

  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) cout << a[i][j] << " ";
    cout << endl;
  }
  return 0;
}

Also, if you really want to read arbitrarily large arrays, then you should use std::vector or some such other container, not raw arrays.

另外,如果你真的想要读取任意大的数组,那么你应该使用std :: vector或其他一些容器,而不是原始数组。