簡體   English   中英

從巨大的txt讀取2d矩陣

[英]Reading 2d matrix from huge txt

我必須為應該從文本文件讀取2d數組的程序編寫函數。 文本文件是可變的,因此我編寫了將計算行數和列數的函數,還編寫了一個顯示矩陣的函數,但是我遇到了麻煩,該函數應該將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;
}

如果您已經知道文件中的nRows行和nColumns列,則代碼應如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM