Reading txt file(different number in each line) and store in a vector

I have a text file that contains 40 lines
Each line contains different number of integers. For example:
17 18
20 33 41 44
50
55 60 66

Is is possible to read all these integers and store them in one vector?

Yes, it is possible to read every integer value from your file and store them in a vector.

Have you already written some code you could share with us, or are you waiting for us to do all the work?
EmanCS wrote:
Is is possible to read all these integers and store them in one vector?


Sure, but what do you plan to do with them when you've got them?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main()
{
   vector<int> V;
   ifstream in( "data.txt" );
   for ( int x; in >> x; ) V.push_back( x );    // read all integers in file into V

   for ( int e : V ) cout << e << ' ';
}


17 18 20 33 41 44 50 55 60 66 
if you did double vector you could keep the file structure, if you care about it?
Topic archived. No new replies allowed.