Know when a file row starts with a given string

Asked

Viewed 33 times

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 this I must add the line content in an array, currently I do so:

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 you inside the while check what type of line I am reading and store in a different array, for example, when the line starts with v i store in matrizV, when you start with f i store in matrizF.

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) { ... }
...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.