I have a textfile with some rows of text
I want to put the text into a 2D vector because I need to be able to call each character seperatly [x][y]
this is what I've got: i get an error on maze[i][j] != "\0"
int main() {
// Variable declarations
fstream file;
int i=0;
vector<vector<char> > maze(1,vector<char>(1));
ifstream myReadFile;
myReadFile.open("input.txt");
while (!myReadFile.eof()) {
for (int j=0; maze[i][j] != "\0"; j++){
myReadFile >> maze[i][j];
}
i++;
}
file.close();
for (int i = 0; i < maze.size(); i++)
{
for (int j = 0; j < maze[i].size(); j++)
{
cout << maze[i][j];
}
}
return 0;
}
i found a solution almost:
#include <fstream>
#include <stdio.h>
#include <string>
#include <sstream>
using namespace std;
template <typename T> //T could be of any data type
void printVector(T dvalue){//print the input data
cout<<"List of the stored values "<<endl;
for(int i=0; i<dvalue.size(); i++){
for(int j=0; j<dvalue[i].size(); j++){
cout<<dvalue[i][j]<<" ";
}
cout<<endl;
}
}
int main(int argc, char* argv[]){
cout<<"This example utilizes multi-dimensional vectors to read an input data file!"<<endl;
vector< vector<string> > dvalue; //multidimensional vector declaration
string line;
fstream myfile; //define a myfile object of fstream class type
myfile.open("input.txt"); //open the file passed through the main function
//read 1 line at a time.If the data points are seperated by comma it treats as a new line
while(getline(myfile,line,'\n')){
vector <string> v1;
istringstream iss(line);//read the first line and store it in iss
string value;
//this line breaks the row of data stored in iss into several columns and stores in the v1 vector
while(iss>>value){
v1.push_back(value);
}
dvalue.push_back(v1);//store the input data row by row
}
printVector(dvalue);
return 0;
}
But this one can t handle mutiple spaces, it outputs them as one why ?