Question

I am trying to parse the faces from an Wavefront OBJ file. These faces are specified in the file one by line, having the following format:

f v1/vt1/vn1 v2/vt2/vn2 ...

The sequence of triplets (v, vt, vn) is variable in length. Each triplet is separated by a space and each value inside a triplet is separated by a /.

So far so good, using strtok to separated the triplets and then parsing each triplet with sscanf and the format %f/%f/%f works just fine. However in the specifications of the OBJ files, this line can have an alternative form, like this:

f v1//vn1 v2//vn2 ...

In this case the vt is missing and should be missing from all the triplets and the sscanf parse is not going to work.

Is there any way I can parse this in C or C++ in order to avoid nested strtok?

Was it helpful?

Solution

I guess you could first use the %f//%f format, and check the return value. If the return is less than two, do the %f/%f/%f format instead, which should work with both v, vt, and vtn.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top