I'm trying to extract faces and then get the respective vertex indices from the .obj
file as follows. I can now print the vertex indices separately using the strtok()
function, but I can't seem to find the correct way to parse it.
Here's my code:
#include <stdio.h>
#include <string.h>
//lazy wavefront obj file parser
int main(){
FILE *fp = fopen("head.obj", "r");
//find the number of lines in the file
int no_lines = 0;
char ch;
while(ch != EOF){
ch = getc(fp);
if(ch == '\n')
no_lines++;
}
printf("number of lines: %d\n", no_lines);
//set seek point to start of the file
fseek(fp, 0, SEEK_SET);
//get the faces and parse them
char line[100];
while(fgets(line, sizeof(line), fp) != NULL){
if(line[0] == 'f'){
//split the line at spaces
char *s = strtok(line, " ");
while(s != NULL){
if(*s != 'f'){
/*this will print faces as follows
58/35/58
59/36/59
60/37/60
we need to get the first number from each line, i.e., 58, 59, 60
*/
printf("%s\n", s);
}
s = strtok(NULL, " ");
}
}
}
fclose(fp);
return 0;
}
The output looks something like this.
1202/1248/1202
1220/1281/1220
1200/1246/1200
1200/1246/1200
1220/1281/1220
1198/1247/1198
1201/1249/1201
1200/1246/1200
1199/1245/1199
1201/1249/1201
1202/1248/1202
1200/1246/1200
I want to extract the numbers from the above output as follows, all I need are the first numbers from each line.
For the following lines
1202/1248/1202
1220/1281/1220
1200/1246/1200
the output should be 1202, 1220, 1200
.