如何将文本文件的未知内容读入二维数组

时间:2010-11-02 16:49:45

标签: c++ arrays

我正在尝试将文本文件的未知内容读入2D数组,并使其看起来像:

M [0] [0]=2 M [0] [1]=1 M [0] [2]=0
M [1] [0]=0 M [1] [1]=1 M [1] [2]=3
M [2] [0]=8 M [2] [1]=9 M [2] [2]=1
M [3] [0]=3 M [3] [1]=5 M [3] [2]=2

当文本文件如下所示:

2
1 0
0 1
3
8 9 1
3 5 2
-2 3 -1
0

末尾的零显示文件的结尾。

我的问题是数组的最大大小可能是10X10,所以无法知道2D数组的大小是多少,以及如何让它看起来像我上面所示。

有什么想法吗?

6 个答案:

答案 0 :(得分:1)

对于某些尺寸N x M

char c;
int x;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
  c = fgetc(file);
  if(c == ' ') continue;
  if(c == EOF) return;
  if(c == '-')
  {
      c = fgetc(file);
      x = -1 * ((int)c);
  } else {
      x = c;
  }
  if(x == 0)
  {
      array[i][j] = x;
  } else {
      return;
  }
}
}

但如果你在谈论“存储这些矩阵所需的矩阵大小是多少”那么你需要一种方法来找出你想要的尺寸。

答案 1 :(得分:1)

只需使用'fstream'即可。它忽略了新的线条,就像'iostream'一样。您只需跟踪矩阵行和列。

//open "myFileName.txt" with an input file stream
std::ifstream inputFile("myFileName.txt");

while(!inputFile.eof()) //Check for end-of-file character
{
    //do this while keeping track of your matrix position
    inputFile >> M [curRow] [curColumn]
}

inputFile.close();

不要忘记包括图书馆:

#include <fstream>

编辑:>>运算符也会尝试将输入自动转换为您使用的任何类型:

double dTemp;
int iTemp;
std::string sTemp;

std::ifstream in("myFile.txt");

in >> dTemp; //input cast as double
in >> iTemp; //input cast as integer
in >> sTemp; //input cast as string

in.close();

编辑:获取文件的元素数量

int temp, numberOfInputs;
while(!inputFile.eof())
{
    inputFile >> temp;
    ++numberOfInputs;
}

inputFile.seekg(0, std::ios::beg); //Go to beginning of file

获得输入数量后,您可以使用它来计算行数和列数。

答案 2 :(得分:1)

尝试:

#include <vector>
#include <fstream>
#include <iostream>


int main()
{
    std::ifstream   file("plop.dat");
    if(!file)
    {
        std::cerr << "Failed to open File\n";
        return 1;
    }

    // Keep looping until we are done.
    while(true)
    {
        int size;
        file >> size;

        // You said a size zero indicates termination.
        if (size == 0)
        {    break;
        }

        // Create a square 2D vector (Vector inside a Vector)
        std::vector<std::vector<int> >      matrix(size, std::vector<int>(size, 0));

        // Loop over each axis
        for(int x = 0;x < size; ++x)
        {
            for(int y = 0;y < size; ++y)
            {
                // Read one number for each location.
                // The operator >> automatically skips white space
                // White Space includes newline and tab. So it should work
                // perfectly if the input is OK. But it is hard to detect
                // an error in the format of the file.
                file >> matrix[x][y];
            }
        }
    }
}

答案 3 :(得分:0)

所以数组的所有行都有3个值? 只需将值读入数组,保留您所在列的数量,并忽略换行符?

看看getline

答案 4 :(得分:0)

getlinestringstream阅读,vector< vector<int> >存储。

编辑:哦,所以尺寸总是N * N?然后只需使用while(cin>>x) { if (cin.good()) ...; }阅读vector<int>,查看总大小,然后拆分为vector< vector<int> >

答案 5 :(得分:0)

单维和多维C数组只是连续的内存。根据数组的索引,差异更具有语法性:对于二维数组,它只是将一个维度乘以另一个维度的大小,然后再添加第二个维度以找到偏移量。

所以要解决:

  1. 将值读入std :: vector(其他答案已经提到了这样做的方法)
  2. 计算出大小(myArray.size()的整数平方根)
  3. 计算索引,例如:

    int idx(int size, int d1, int d2)
    {
        return (d1*size)+d2;
    }
    
  4. 返回索引处的向量元素,例如:

    for (int d1 = 0; d1 < size; d1++)
    {
        for (int d2 = 0; d2 < size; d2++)
            std::cout << "M [" << d1 << "] [" << d2 << "]=" << myArray[idx(size, d1, d2)] << " ";
        std::cout << std::endl;
    }
    
  5. 给了我:

    $ g++ arr.cpp && ./a.out
    M[0][0]=2 M[0][1]=1 M[0][2]=0
    M[1][0]=0 M[1][1]=1 M[1][2]=3
    M[2][0]=8 M[2][1]=9 M[2][2]=1