First, on handling the lines separately:
Example:
if (myfile.is_open())
{
short line_num = 1;
for (std::string line; std::getline(myfile, line); ) {
cout << "Line number: " << line_num << " Value: " << line << std::endl;
line_num++;
}
}
Output:
mindaugasb@c_cpp:~/workspace/StackOverflow/ReadingAFileLineByLine $ ./r2v.exe
Please enter file name!
f
Line number: 1 Value: 123, 123, 431, 5123, 12312
Line number: 2 Value: 25316, 64234, 1231, 124123
Second on pushing the lines, item at a time, to a vector:
I will assume there will be two vectors, for the two lines in the input file. If there are more lines, you will need to create more vectors, possibly by implementing a function that will accept as string (obtained from the input file) and return a vector in the form you want it. Example:
vector<string> line2Vec(string line)
{
char * tokens;
char * line_chararr = const_cast< char *>(line.c_str());
tokens = strtok(line_chararr, ", ");
vector<string> vs;
while (tokens != NULL)
{
vs.push_back(tokens);
tokens = strtok (NULL, ", ");
}
return vs;
}
Attention - I do not say that this is perfect Cpp code or a perfect solution to your problem. However it is A solution to how I understood the problem based on what was written. Here is the full version to play around with:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <sstream>
#include <vector>
#include <string.h>
using namespace std;
vector<string> line2Vec(string line)
{
cout << line << endl;
char * tokens;
char * line_chararr = const_cast< char *>(line.c_str());
tokens = strtok(line_chararr, ", ");
vector<string> vs;
while (tokens != NULL)
{
vs.push_back(tokens);
tokens = strtok (NULL, ", ");
}
return vs;
}
int main()
{
string fname;
cout << "Please enter file name!" << endl;
cin >> fname;
fstream myfile;
myfile.open(fname);
if (myfile.is_open())
{
for (std::string line; std::getline(myfile, line); ) {
line2Vec(line);
}
}
else
{
cout << "Unable to open your file!" << endl;
}
}