How to separate line by using "strtok"?

I have to read txt file which data is number. For example, my txt file is

"1 2 3 4
2 3 4 5 6 7"

that is it has to rows and each row has different number of column.

I'd like to keep it in 2 dimentional array,
vector< vector<float>> vec(2,vector<float>). So I use function "strtok" to seperate my data when it find " ", and use function "atof" to convert char into float. However, when I try to puch the number into my vector, it continue to push all data into one row. My vector now is [1 2 3 4 2 3 4 5 6 7]. How can I write my code to start a new row when it goes to the end of previous row. I hope my vector looks like
[1 2 3 4
2 3 4 5 6 7]


My code is

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#include<fstream>
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<cmath>
#include<vector>



using namespace std;

int main(){

char input_buffer[1000];

ifstream inf("testdata.txt");
vector < vector<float> > vec(2, vector<float>());

while (inf)
{
inf.getline(input_buffer,1000);
cout << input_buffer << endl;
char * pch;
pch = strtok (input_buffer," ");
for (int i = 0; i < 2; i++)
{
while ((pch != NULL))
{
vec[i].push_back(atof(pch));
pch = strtok(NULL, " ");
}
}
}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Here's an idea:
first split the string in two parts using strtok where delimiter = '\n'
then split each of them with strtok where delimiter = ' '

edit: by the way,
don't mix c and c++ libraries that do the same thing, http://www.cplusplus.com/forum/articles/17108/ , main should return 0, this forum has [code] tags
Last edited on
Why so complicated? Just read the numbers as numbers.
To identify a row you could read the line in a string, pass it to a stringstream and then read as numbers.
Thanks you guys so much.
Topic archived. No new replies allowed.