strtok problem

This might not be entirely appropriate as it's c and not c++, but I'm guessing that there are people from both camps here:p I'm trying to read mesh-data from an obj-file in order to draw it using OpenGL and glut. A line from the obj-file might look like this:

v 1.000000 -1.000000 -1.000000

indicating that there's a vertex with coords x=1, y=-1, z=-1. I'm trying to read it like this:

1
2
3
4
5
6
7
8
9
while(fgets(line, max_buf, fp) != NULL) {
		token = strtok(line, " ");
		if (line[0] == 'v') {
			token = strtok(NULL, " ");
			m->vertices[v_index++].x = atof(token);
			token = strtok(NULL, " ");
			m->vertices[v_index].y = atof(token);
			token = strtok(NULL, " ");
			m->vertices[v_index].z = atof(token);


This almost works fine, the y and z coords are read fine, but x is always 0! I haven't really used strtok before, so I'm guessing it has something to do with that. Does anyone have any ideas?
Perhaps you need to use m->vertices[++v_index] instead of the post-increment? As written, your x is stored in a different element of the vertices array.
Wow, I'm blaming that on being tired:p Thanks, I moved the increment to after reading the vertices, it works now :)
Last edited on
Topic archived. No new replies allowed.