How do you parse a file into a 2D vector? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do you parse a file into a 2D vector?

How do you read in any text file into a 2D vector? This text files has characters in it.

12th Feb 2018, 6:57 AM
Guada Guap
Guada Guap - avatar
3 Answers
+ 2
Maybe you wanted to do this? : fstream fin; char ch; string name; //File Name. vector<vector<char>> vec; // 2D Vector. vector<char> temp; // Temporary vector to be pushed // into vec, since its a vector of vectors. fin.open(name.c_str(),ios::in); // Assume name as an arbitary file. while(fin) { ch = fin.get(); if(ch!='\n') temp.push_back(ch); else { vec.push_back(temp); temp.clear(); } } fin.close(); for(vector<char> v:vec) // Print the 2D Vector contents. { for(char c:v) cout<<c; cout<<endl; }
12th Feb 2018, 2:07 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
Which language? What data is in the file? Do you want to use a parser or your own code? Note: 2D vectors are called matrices
12th Feb 2018, 10:07 AM
spcan
spcan - avatar
+ 2
In c++
12th Feb 2018, 12:45 PM
Guada Guap
Guada Guap - avatar