1

I'm having slight trouble creating a 2D Vector of String that's created by reading values from a text file. I initially thought I needed to use an array. however I've come to realise that a vector would be much more suited to what I'm trying to achieve.

Here's my code so far:

I've initialised the vector globally, but haven't given it the number of rows or columns because I want that to be determined when we read the file:

vector<vector<string>> data;

Test data in the file called "test" currently looks like this:

test1 test2 test3
blue1 blue2 blue3
frog1 frog2 frog3

I then have a function that opens the file and attempts to copy over the strings from text.txt to the vector.

void createVector()
{
    ifstream myReadFile;
    myReadFile.open("text.txt");

    while (!myReadFile.eof()) {
        for (int i = 0; i < 5; i++){
            vector<string> tmpVec;
            string tmpString;

                for (int j = 0; j < 3; j++){
                myReadFile >> tmpString;
                tmpVec.push_back(tmpString);
                }
            data.push_back(tmpVec);
        }
    }
}

However, when I attempt to check the size of my vector in my main function, it returns the value '0'.

int main()
{
  cout << data.size();
}

I think I just need a pair of fresh eyes to tell me where I'm going wrong. I feel like the issues lies within the createVector function, although I'm not 100% sure.

Thank you!

  • [Please read why using eof() in a loop is not good](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – PaulMcKenzie Jan 02 '17 at 03:53
  • *but haven't given it the number of rows or columns because I want that to be determined when we read the file:* -- So why did you hard-code `5` and `3` in your `createVector` function? – PaulMcKenzie Jan 02 '17 at 03:55
  • Thanks for your reply Paul. I know that the maximum number of columns there will be is 3, however I don't know the number of rows (because this is something that can be altered by other functions in the program - i.e. add and removing elements). – GuestUser140561 Jan 02 '17 at 03:57
  • 1
    umm, could it be that you did not call the function `createVector` at all, as I see in your `main` ?? – A.S.H Jan 02 '17 at 04:18

1 Answers1

1

You should use std::getline to get the line of data first, then extract each string from the line and add to your vector. This avoids the while -- eof() issue that was pointed out in the comments.

Here is an example:

#include <string>
#include <iostream>
#include <vector>
#include <sstream>

typedef std::vector<std::string> StringArray;

std::vector<StringArray> data;

void createVector()
{
    //...
    std::string line, tempStr;
    while (std::getline(myReadFile, line)) 
    {
        // add empty vector
        data.push_back(StringArray());

        // now parse the line 
        std::istringstream strm(line);
        while (strm >> tempStr)
            // add string to the last added vector
            data.back().push_back(tempStr);
    }
}

int main()
{
    createVector();
    std::cout << data.size();
}

Live Example

PaulMcKenzie
  • 31,493
  • 4
  • 19
  • 38