从巨大的txt读取2d矩阵

时间:2012-12-03 10:41:56

标签: c++ arrays file matrix

我必须为一个程序编写一个函数,以便从文本文件中读取一个二维数组。文本文件是可变的,所以我编写了将计算行数和列数的函数,我还编写了一个显示矩阵的函数,但我遇到了应该将txt文件中的值读入2d的函数的问题阵列。 我正在粘贴我所做的事情。请记住,我已经在线寻找我的问题的答案4天。 所以我需要帮助的是从txt获取值到矩阵的函数。谢谢。

#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <fstream>
#include <istream>
#include <iomanip>
#include <sstream>
#include <string>
#include <stdio.h>
using namespace std;

/*Declaring unviversal variables*/

ifstream datafile ("randaex1.txt");             /*Original data file*/
ifstream testfile ("test1.txt");
ofstream outputfile;                            /*Output     data file*/
string line,a,b,auxiliar;
stringstream ss;
int number_of_lines,col,matrix[100][15000];
int A[4][5] = {{1, 2, 3, 4, 5},
           {6, 7, 8, 9, 10},
           {11, 12, 13, 14, 15},
           {16, 17, 18, 19, 20}};



int readfromfile()                              /*Test     function. Reads the first line of the file into variable "line"*/
{
if (datafile.is_open())
{
    getline (datafile,line);
    datafile.close();
}
else
    cout << "Unable to open file";
return 0;
}

int writetofile ()                              /*Test     function. Writes "line" to the output file.*/
{
outputfile.open ("example.txt");
outputfile << line << "\n";
outputfile.close();
cout << "\nThe line\n \n" << line << "\n\nwas copied to example.txt" << endl;           /*Gives feedback of the copied line to user.*/
return 0;
}

int sizeofline()                            /*Test function.     Calculates the size of a line*/
{
cout << "\nThe size of line is " << line.size() << " characters.\n";
return line.size();
}

int linesinfile()                           /*Calculates the     number of lines in datafile(the number of rows in matrix)*/
{
number_of_lines=0;
std::string liney;
std::ifstream myfile("randaex1.txt");

while (std::getline(myfile, liney))
{
    ++number_of_lines;
}

std::cout << "Number of lines in text file: " << number_of_lines;

system("pause");
return number_of_lines;
}


int columns()                           /*Calculates the number of columns in datafile*/
{

col=0;
for (int i=0;i<=line.size();i++)
    if (line[i]==' ')
        col++;
col = col++;
cout << col;
return col;
}

void Readm(int N, int M)
{
for(int R=0;R<N;R++)
{
    for(int C=0;C<M;C++)
        cin>>A[R][C];
    cout<<endl;
   }
}



void Displaym(int N, int M)
{
N=4, M=5;
for(int R=0;R<N;R++)
{
    for(int C=0;C<M;C++)
        cout<<setw(10)<<A[R][C];
    cout<<endl;
   }
}

void matrixfile()                       /*Reads file to matrix*/
{
string str[10][5];
int a = 0;
int b = 0;
if(!testfile) //Always test the file open.
{
            cout<<"Error opening output file"<<endl;
            system("pause");

}
while(!testfile.eof())
{
  getline(testfile,str[a][b],' ');
  cout << str[a][b];
  system("pause");
  if(a == 5)
  {
       a=0;
       ++b;
       getline(testfile,str[a][b],' ');
       cout << str[a][b];
       system("pause");
  }
  a++;
    }
b=0;
for(int i=0; i<=3; i++)    //This loops on the rows.
{
    for(int j=0; j<=4; j++) //This loops on the columns
    {
        cout << str[i][j]  << " ";

    }
    cout << endl;
}
}



int main()
{
readfromfile();
writetofile();
sizeofline();
getchar();
/*linesinfile();*/
getchar();
columns();
getchar();
Displaym(4,5);
getchar();
return 0;
}

1 个答案:

答案 0 :(得分:0)

如果您已经知道nRows行和列中的文件,那么您的代码应该是这样的:

void ReadFileToMatrix()
{
    datafile.seekg(0, ios::beg);

    for(int i = 0; i < nRows; i++)
        for(int j = 0; j < nColumns; j++)
            datafile >> matrix[i][j]; 
}