c++ - 从巨大的 txt 中读取二维矩阵

标签 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;
}

最佳答案

如果你已经知道在你的文件中有 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]; 
}

关于c++ - 从巨大的 txt 中读取二维矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13681547/

相关文章:

python - 在没有并行化的情况下提高功能的性能

python - 如何编写一个python脚本在指定时间自动复制文件而不需要用户手动执行?

c - 使用C直接读/写磁盘

C++ 映射将所有键的值显示为 0

c++ - 在 Ubuntu 20.04 中为 RPI4 链接 WiringPi 共享对象库

c++ - 旧版 MySQL 连接器 C++ 使用 sql::ConnectOptionsMap 异常

java - 尝试使用方法创建驾驶执照考试程序

javascript - 对 html 中选定的值求和

c++ - 如何找出变量的类型?

c++ - 以二进制文件打开文件与以文本文件打开文件之间的区别