#include <sstream>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
void put_into_vector( ifstream& ifs, vector<int>& v )
{
string s;
getline( ifs, s );
istringstream iss( s );
// not the fastest method ...
copy( istream_iterator<int>( iss ), istream_iterator<int>(), back_inserter( v ) );
}
int main()
{
vector<int> line_1, line_2;
ifstream ifs( "C://data.txt" );
put_into_vector( ifs, line_1 );
put_into_vector( ifs, line_2 );
}
Is there a way to read automatically each line of the data.txt depending on how many lines the file has?
I can get the number of lines that it has with:
int line_count = 0;
char ch;
ifstream iFile("c:/data.txt");
if (! iFile)
{
cout << "Error opening input file" << endl;
system("pause");
return -1;
}
while (iFile.get(ch))
{
switch (ch) {
case '\n':
line_count++;
break; }
cout << line_count;
Thanks in advance