Chained list prints beyond what is inserted

Asked

Viewed 82 times

0

I have that function

verticeobj* loadverticeObj(char *fname,verticeobj *vo){
    FILE *fp;
    int read;
    float x, y, z;

    char ch;
    fp=fopen(fname,"r");
    if (!fp)
    {
        printf("can't open file %s\n", fname);
        exit(1);
    }
        while(!(feof(fp)))
        {
            read=fscanf(fp,"%c",&ch);

            if(read==1&&ch=='v')
            {
                read=fscanf(fp,"%f %f %f",&x,&y,&z);

                vo =inserevertice(vo,x,y,z);
            }
            else
            {
                if(ch=='f')
                {
                    read=fscanf(fp,"%d %d %d %d",&x,&y,&z,&ch);
                }
            }
        }
    fclose(fp);

    return vo;
}

Which takes the chained list of type verticeobj and returns the filled list. Within the function, when the condition is met, an insertion function is called by rerunning the list to enter the data into the list. The insertion function is:

verticeobj* inserevertice(verticeobj *lv, float x, float y, float z){
    verticeobj* novo = (verticeobj*)malloc(sizeof(verticeobj));
    novo->x = x;
    novo->y = y;
    novo->z = z;
    novo->prox = lv;
    lv = novo;

    return lv;
}

The point is that in the main when I call a function to print the list, it prints the desired elements by putting two more lines of junk that I don’t know for sure what it is.Within the insertion function I put a printf to test and it prints all elements correctly. From now on, thank you

  • Break function fscanf upon reading chars mixed with floats and intIt’s just too easy. Using this function is something that tends to be somewhat fragile because it only works well when the data is already properly formatted, producing horrific things when data outside the expected pattern is found. How’s the file you’re reading?

  • so "v 25.0 15.0 25.0v 15.0 15.0 25.0f 1 2 3 4f 5 6 7 8" for example

  • Is there a line break in this? Or do we really have 25.0f with the f glued to the 25.0?

  • the file was with line break but did not change the final result

1 answer

0

I solved the problem was that I was not initiating the lists chained with NULL

Browser other questions tagged

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