This should tokenize space delimited fields in the string str:
float values[2*linesnum(str, length)];
char * pch;
pch = strtok(str, " ");
while (pch != NULL) {
printf("%s\n", pch);
pch = strtok(NULL, " ");
}
linesnum() just counts the number of ‘\n’ characters in a string. The above code correctly prints all values from str that I expect.
However, this prints only the first value, and then every second value thereafter:
int k = 0;
float values[2*linesnum(data, length)];
char * pch;
pch = strtok(data, " ");
while (pch != NULL) {
values[k] = atof(pch);
//printf("%s\n", pch);
printf("%f\n", values[k]);
pch = strtok(NULL, " ");
k++;
}
Here is an example of what the input string looks like:
3.31 2.16
4.28 0.56
7.20 3.09
11.18 5.06
In the first code, it will print out:
3.31
2.16
4.28
0.56
7.20
3.09
11.18
5.06
In the second code, it will print out:
3.310000
2.160000
0.560000
3.090000
5.060000
I must be doing something silly. Is it putting only half of the values into the array? Why, if all the values show up when I directly print the string pch? It is as if the single command pch = strtok(NULL, " "); is producing two output, and I can capture only one.
The problem appears to be that your string looks something like this:
"1.234 5.678\n9.012 3.456", that is, the second and third numbers (and the fourth and fifth numbers, and so on) are separated by a'\n'character, instead of a' 'character. To remedy this, you can just add'\n'to your string of delimiter characters:Just a note: the reason your original code appears to work is that the second value read is actually (in my example)
"5.678\n9.012", which, when printed, looks likeEven though it looks like two values, in reality it is only one string.