Know when a file line starts with a given string

0

I have a file like this:

# Blender v2.69 (sub 0) OBJ File: 'CUBO.blend'
# www.blender.org
mtllib cube.mtl
o Cube.022_Cube.030
v 0.450000 -1.450000 0.550000
v 0.450000 -1.450000 1.450000
v -0.450000 -1.450000 1.450000
v -0.449999 -1.450000 0.550000
v 0.450000 -0.550000 0.550000
v 0.449999 -0.550000 1.450000
v -0.450000 -0.550000 1.450000
v -0.450000 -0.550000 0.550000
usemtl Material
s off
f 5 8 7 6
f 1 5 6 2
f 3 7 8 4
f 5 1 4 8
usemtl Material.026
f 2 6 7 3
usemtl Material.047
f 1 2 3 4

How do I identify when the line starts with o v usemtl or f ? Because depending on that I should add the contents of the row in an array, I currently do this like this:

float **readVertices(char *filename)
{
    int lines = fileLines(filename), li;
    float **matriz = createArrayFloat(lines, 3);

    FILE *file;

    file = fopen(filename, "r");

    if(file == NULL)
            printf("Erro, nao foi possivel abrir o arquivo\n");
    else {
        int count = -1;
        do {
            count ++;
        }while(fscanf(file, " v %f %f %f", &matriz[count][0], &matriz[count][1], &matriz[count][2]) != EOF);
    }
    fclose(file);
    return matriz;
}

But I want within while to check what type of line I'm reading and store in a different array, for example, when the line starts with v I store in matrizV , when it starts with f I store in matrizF .

    
asked by anonymous 09.11.2016 / 16:06

1 answer

1

You can read the initial string and, depending on the content, take a different action.

char aux[16];
fscanf(arq, " %s", aux);
if      (strcmp(aux,      "o") == 0) { ... }
else if (strcmp(aux, "usemtl") == 0) { ... }
...
    
09.11.2016 / 16:33