David
David

Reputation: 43

How can I transfer a text file to array 2D in C++?

How can i transfer a text file(.txt) to array 2D in c++ and this is my source code

fstream fin('textfile.txt', ios::in);
int matrix[n][m]; // n is the number of rows and m is the number of columns
for(int i = 0;i < n; i++){
     for(int j = 0; j<m; j++){
     fin>>matrix[i][j];
     }
}

but how can i detemine n and m for do this,i need your help and your advice, please Join us your perspectives

Upvotes: 1

Views: 441

Answers (2)

Rishabh
Rishabh

Reputation: 62

So u mean u want to read a file and copy contents to char Matrix[][]??, you can use while loop to read characters, and split every 1024 bytes(1 kb) by lines, i mean do this:

#include <fstream.h>

int main()
{
int n=0, m=0, MAX_LINES=1025, i=0, j=0;
char character, Matrix[1024][1025];
fstream file;
file.open("file.txt", ios::in);
while (file.get(character))// loop executes until the get() function is able to read contents or characters from the file
{
Matrix[n][m]=character; // copy the content of character to matrix[n][m], here n is used to work as lines and m as normal arrays.
m++; // finished copying to matrix[n][m] then m++ else the program will overwrite the contents to the same array.
If (m>=1024)// if string[n][m] reached the limit which is 1024.
{
Matrix[n][m]='\0'; //termimate that line
m=0;// start with a new column
if (n<MAX_LINES)// if n is less than 1024 then
n++;// write to a next line because m  can support only 1024 chars.
}
}
Matrix[n][m]='\0';// this will terminate the whole string not just one line.
file.close();
for (i=0; i<1025; i++)
{
for (j=0; j<=1024 || Matrix[i][j]!='\0'; j++)
cout<<Matrix[i][j];
}
return 0;
}

This code will read 1024×1024 chars, but if the txt file is less than 1024(m) characters, the while loop will be exited and Matrix[n][m]='\0'; statement is executed.

EDIT: As asked by David i have written the whole code with main() sorry bro i forget, the bug in the code was the variables n and m were intialised to 1025 and 1024 so the program skips writing to Matrix as the Matrix[1024][1025] cannot store more characters... I think this would help, ok bro...

Upvotes: 0

Surt
Surt

Reputation: 16099

This solution requires C++11+

If there is no n & m in the file, you must assume that the layout is also in 2D

One Two Three
Four Five Six

Warning:: Untested code.

std::stringstream res;
std::string wordUp;
std::vector<std::string> str;

// the matrix, vector of vector of strings.
std::vector<std::vector<std::string>> matrix;  

fstream fin('textfile.txt', ios::in);
int lines = 0;
int words = 0;

// read the file line by line using `getline` 
for (std::string line; std::getline(fin, line); ) {
  ++lines;
  // use stringstream to count the number of words (m).
  res.str(line); // assign line to res. might also need some reset of good().
  while (res.good()) {
    res >> wordUp;
    str.push_back(wordUp);
    ++words;  
  }
  matrix.push_back(str);
  str.erase(str.begin());
}

Upvotes: 1

Related Questions