2010-11-02 63 views
0

我想读取文本文件的未知内容到一个二维数组,并让它出来找这样的:如何读取文本文件的未知内容到一个二维数组

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的最大大小,因此无法知道二维数组的大小是什么,以及如何得到它看起来像我上面显示的方式。

任何想法?

+0

是否阵已是N×N的?否则,对于某些输入有多个有效的数组维度......哪一个可以选择? – delnan 2010-11-02 16:52:51

+0

也许我只是误解了你的意图,但我在你的矩阵显示中看到M [2] [0],M [2] [1]和M [2] [2]两次,而不是-2,3和 - 1个文件示例中的值。如果你想帮助人们理解你的问题,你应该清除它。 – haylem 2010-11-02 17:08:20

+0

你也应该知道,用0表示文件的结尾意味着你必须有扩展的逻辑来确定矩阵体内的0是否是一个真正的零或被排除的“结束”零。 – SubSevn 2010-11-02 17:13:52

回答

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 

一旦你有了输入的数量,你可以用它来计算出行数和列数。

+0

这将工作,如果我知道文件的大小和多少行/列我想,但我不知道。有一个10X10的最大值,我可以采取任何事情。 – Zud 2010-11-02 17:10:57

+0

@Alec - 先阅读文件的内容,然后阅读内容。我会更新答案。 – 2010-11-02 17:20:41

+0

不要在while条件下测试eof。当你进入循环时,即使你已经到达文件的末尾。 – 2010-11-02 18:30:07

0

所以数组中的所有行有3个值?
只需读取的值放入数组保存你是哪一列,并忽略换行符计数?

看函数getline

+0

这些行都需要以相同的长度结束,特殊值标记文件的结尾。它需要看起来像一个矩阵。 – Zud 2010-11-02 16:51:57

+0

你有什么工作,但输入可以是每行最多10个值的任何东西。在从文件中获得输入之前无法知道。 – Zud 2010-11-02 16:53:20

+0

矩阵并不总是“正方形”,你可以有一个(例如)1x100并存储100个条目,或者10x10做同样的事情。 – SubSevn 2010-11-02 16:55:10

0

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

编辑:哦,所以大小始终是N * N?然后,只需读取使用while(cin>>x) { if (cin.good()) ...; }vector<int>,检查总规模,并分割成vector< vector<int> >

1

某些维度N×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; 
    } 
} 
} 

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

+0

这就是我想要读取的数组,但如果我这样做,所有这些数字将结束在前两行,我不会得到任何地方。 – Zud 2010-11-02 16:56:26

+0

就是这样,你必须知道你想要什么尺寸。我会清理一下代码。 – SubSevn 2010-11-02 16:57:30

+0

迈克韦伯提到的对' - '字符(和其他东西)进行一些修改的fstream方法是一种更加C++的方法(使用流而不是fgetc)。这和我的代码没有测试heh .. – SubSevn 2010-11-02 17:10:10

0

单和多维C数组只是连续的存储器块。在索引到数组的过程中,差异更具语法:对于二维数组,它只是将一个维乘以另一维的大小,然后再添加第二维以查找偏移量。

所以要解决:

  1. 读值到一个std ::向量(其他答案已经sugegsted的方式来做到这一点)
  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; 
    } 
    

给我:

$ 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 
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]; 
      } 
     } 
    } 
} 

相关问题