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!

4
  • Please read why using eof() in a loop is not good Commented Jan 2, 2017 at 3: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? Commented Jan 2, 2017 at 3: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). Commented Jan 2, 2017 at 3:57
  • 1
    umm, could it be that you did not call the function createVector at all, as I see in your main ?? Commented Jan 2, 2017 at 4:18

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for taking the time to explain Paul, it really cleared things up for me! Got it working perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.