-2

I would like to insert each row into vector.

My file looks like this:

file

The problem is to use multiple delimiters with getline. My code looks like this :

vector<int>czytajplik()
{
string line;
vector<string> text;
ifstream mojplik;
vector<vector<int>> zajecia;


mojplik.open("dane.txt");
if (mojplik.is_open())
{
    while (getline(mojplik, line))
    {

        while (getline(mojplik, line, ' '))
        {
            text.push_back(line);
        }
    }
    mojplik.close();
}

else cout << "Unable to open file" << endl;


vector<int> textnumery;
for (int i = 0; i <text.size(); i++)
{
    int num = atoi(text.at(i).c_str());
    textnumery.push_back(num);
}
return textnumery;

}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
warmeen
  • 3
  • 1
  • 3
    Welcome to stackoverflow. Please clarify your question! – monamona Jan 15 '18 at 13:15
  • Those foreign variable names sure are disturbing ! But the main problem is the question looks too much like "debug my code". Well, if I can still at least help a bit, the two nested while won't work (basically, you're throwing away the first line, then reading the first number of the second line and so on). A way you may achieve what (I assume) you want is to use stringstreams : https://ideone.com/6Ct1U7 another would be to read character after character and code your own automata I guess, there might be other simpler ways – Caninonos Jan 15 '18 at 13:25
  • 1
    You can use the solutions from [How can I read and parse CSV files in C++](https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c) by just substituting a space for the comma. – Bo Persson Jan 15 '18 at 13:43

2 Answers2

1

I'm going to assume that you have data that looks something like

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

and want to have a 2d vector equal to

vector<vector<int>> result = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15 } };

Then you want something like

vector<vector<int>> zajecia;
ifstream mojplik("dane.txt");
if (mojplik.is_open())
{
    for (string line; getline(mojplik, line); )
    {
        stringstream numbers_stream(line);
        vector<int> numbers;
        for (int number; numbers_stream >> number; )
        {
            numbers.push_back(number);
        }
        zajecia.push_back(numbers);
    }
    mojplik.close();
}
Caleth
  • 52,200
  • 2
  • 44
  • 75
  • Note that you can condense the body of the inner loop into `stringstream numbers_stream(line); zajecia.emplace_back( istream_iterator( numbers_stream ), istream_iterator() );`, but I chose to limit what I changed from your original code – Caleth Jan 15 '18 at 13:46
0

I think this is what your asking for:

std::ifstream mojplik{"dane.txt"};
if (mojplik.is_open()) {

    std::vector<std::vector<int>> numbersLines{};

    for (std::string line; getline(mojplik, line);) {
        std::vector<int> numbersLine{};
        std::istringstream is(line);
        int num;
        while (is >> num) {
            numbersLine.push_back(num);
        }
        numbersLines.push_back(numbersLine);
    }
    mojplik.close();
}
fab
  • 1,189
  • 12
  • 21