数组打印正常,直到使用fgets,然后打印0' s。为什么?

时间:2017-02-12 23:18:50

标签: c printf fgets

这是有问题的功能:

int load_off_mesh(FILE *file, jmesh *jm){
    int checker = 0;
    int counter = 0;
    char str[256];
    int trash = 0;
    char *token;
    char *verts;
    char offstr[10];
    int index = 0;
    int triangle = 0;
    if(checker == 0){
        fscanf(file, "%s ", offstr);
        if(strcmp(offstr, "OFF")!= 0){
            printf("File is not in OFF format");
        }
        checker++;
    }
    if(checker == 1){
        fscanf(file, "%d %d %d ", &jm->nvert, &jm->ntri, &trash);
        jm->vertices = (float *)malloc(sizeof(float)*jm->nvert);
        jm->triangles = (int *)malloc(sizeof(int)*jm->ntri);
        checker++;
    }
    while(index < jm->nvert){
        fscanf(file, "%f %f %f ", &(jm->vertices[index*3]), &(jm->vertices[index*3+1]), &(jm->vertices[index*3+2]));
        printf("%f %f %f \n", jm->vertices[index*3], jm->vertices[index*3+1], jm->vertices[index*3+2]);
        index++;
    }
    index = 0;
    while(index < jm->ntri){
        if((fgets(str, sizeof(str), file))!=NULL){
            counter = 0;
            token = strtok(str, " \n\r");
            verts = strdup(token);
            while(token){
                token = strtok(NULL, " \n\r");
                if(token == NULL) break;
                triangle = atoi(token);
                if(atoi(verts) == 3 && counter < 3){
                    jm->triangles[index*3+counter] = triangle;
                    counter++;
                }
                if(atoi(verts) == 4 && counter < 4){
                    if(counter == 0){
                        jm->ntri++;
                        jm->triangles = (int *)realloc(jm->triangles, sizeof(int)*jm->ntri);
                        jm->triangles[index*3] = triangle;
                        jm->triangles[index*3+3] = triangle;
                    }
                    if(counter == 1){
                        jm->triangles[index*3+1] = triangle;
                    }
                    if(counter == 2){
                        jm->triangles[index*3+2] = triangle;
                        jm->triangles[index*3+4] = triangle;
                    }
                    if(counter == 3){
                        jm->triangles[index*3+5] = triangle;
                        index++;
                    }
                    counter++;
                }
            }
            index++;
        }
    }
    index = 0;
    int i = 0;
    while(i < jm->nvert){
        printf("%f %f %f\n", jm->vertices[i*3], jm->vertices[(i*3)+1], jm->vertices[(i*3)+2]);
        i++;
    }
}

这部分代码可以正确打印。

while(index < jm->nvert){
    fscanf(file, "%f %f %f ", &(jm->vertices[index*3]), &(jm->vertices[index*3+1]), &(jm->vertices[index*3+2]));
    printf("%f %f %f \n", jm->vertices[index*3], jm->vertices[index*3+1], jm->vertices[index*3+2]);
    index++;
}

当我进行同样的打印电话时:

while(i < jm->nvert){
    printf("%f %f %f\n", jm->vertices[i*3], jm->vertices[(i*3)+1], jm->vertices[(i*3)+2]);
    i++;
}

在函数的底部,它打印76个循环的正确值,然后开始打印0&#39; s。不知道发生了什么,有人可以启发我吗?

0 个答案:

没有答案