-4

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 ?

NathanOliver
  • 150,499
  • 26
  • 240
  • 331
Ivan Tarskich
  • 718
  • 6
  • 15

1 Answers1

0

Couple of errors I may see.

  1. You define vector as size of 1, but try to add more elements by using direct index. It causes exception. You shall use push_back to add more elements.

  2. If you want to check end of line, the character is '\r' and '\n';

  3. If you want to compare a char, you use '\0' not "\0".

So here is the suggested code:

int main() {
// Variable declarations
fstream file;
vector<vector<char> > maze;

ifstream myReadFile;
myReadFile.open("input.txt");
vector <char> line;
char c;
while (!myReadFile.eof()) {
    myReadFile.get(c);
    if (c == '\r') 
    {
        myReadFile.get(c);
        if (c == '\n')
        {
            if (line.size() > 0)
                maze.push_back(line);
            line.clear();
            continue;
        }
    }
    line.push_back(c);

}

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;
}
Ethan F.
  • 251
  • 1
  • 7
  • You may also use `getline()`, but you need pre-define a fixed space buffer. Which may have potential problem if the size is not enough. – Ethan F. Apr 21 '16 at 18:04