简体   繁体   中英

C Programming - fprintf and printf in while cicle doesn't work

I'm getting a strange problem with a while cicle inside of a function.

I have to look for the extreme vertices of a .ply model. All the data is stored in a linked list. When I'm done creating the list, I call the findExtremeVertex function, that modifies 6 global variables (leftVertex, rightVertex, downwardVertex, upwardVertex, backVertex and frontVertex).

To see if the values are right (the models I use are a bit too big to control every single line to find the maximum of every vertex) I decided to print every change in the max-min values but, when I try to print them in a file, the file is empty. Why is that? Also, when I saw that the file was empty, I tried to print something directly in the console but that didn't work either.

Here's the code of the funcion:

void findExtremeVertex(Vertex *vertex){
    FILE *modelInfoFile;
    int i = 0;

    ///Giving data to direction-vertices pointers
    leftVertex = malloc(sizeof(Vertex));
    rightVertex = malloc(sizeof(Vertex));
    upwardVertex = malloc(sizeof(Vertex));
    downwardVertex = malloc(sizeof(Vertex));
    frontVertex = malloc(sizeof(Vertex));
    backVertex = malloc(sizeof(Vertex));

    ///Giving the direction-vertices the values of the parameter
    leftVertex = vertex;
    rightVertex = vertex;
    upwardVertex = vertex;
    downwardVertex = vertex;
    frontVertex = vertex;
    backVertex = vertex;

    ///Opening file
    modelInfoFile = fopen(us2, "w");
    if(modelInfoFile == NULL){
        printf("Error in file opening. Exiting.");
        exit(EXIT_FAILURE);
    }

    ///Scrolling the list
    while(vertex->prev != NULL){
        vertex = vertex->prev;

        ///If the given element of the list is more to the right than the global variable,
        ///I assign the values of the element to the global variable
        if(vertex->vertexCoordinates.x > rightVertex->vertexCoordinates.x){
            rightVertex = vertex;
        }

        /**
            I'm omitting the other if constructs because are basically
            the same, but the syntax is correct
        **/

        ///Printing in file the cycle information
        fprintf(modelInfoFile, "********** CYCLE %d **********\n\n", i);
        fprintf(modelInfoFile, "Vertex sx\n");
        fprintf(modelInfoFile, "%1.4f %1.4f %1.4f %1.4f %1.4f %1.4f\n\n", leftVertex->vertexCoordinates.x,
                                                                      leftVertex->vertexCoordinates.y,
                                                                      leftVertex->vertexCoordinates.z,
                                                                      leftVertex->vertexNormals.x,
                                                                      leftVertex->vertexNormals.y,
                                                                      leftVertex->vertexNormals.z);

        /**
            Again, I'm omitting some repetitions but the syntax is correct
        **/
        }
    }

I call this function in another function, but there's no segmentation fault signal, the compiler doesn't tell me anything, the program doesn't crash. I have no clue of the error, except from the fact that the file where I print the infos about the cycles is empty. What am I doing wrong?

There are many problems in your code.

  1. You malloc() 6 variables and never use any of them, and you don't check if malloc() succeeded.

  2. You never call fclose() or fflush() so maybe you are seeing the file before the data is flushed to the disk.

  3. You reassign all the *Vertex (except for rightVertex ) variables after they are malloc() ed to the same pointer vertex which means

    1. You are causing a memory leak.
    2. You are using 6 variables for a single pointer.
  4. All the *Vertex variables are not declared inside the function which means that they are in the global scope, that is very likely a bad design choice. Given the code you posted it's not possible to tell whether or not global variables are the right choice, but 99% of the time they are a bad choice and there is a much more elegant and safe way to do things.

The bold point above is likely the reason why your program is behaving as it is.

The code

leftVertex = vertex;
rightVertex = vertex;
upwardVertex = vertex;
downwardVertex = vertex;
frontVertex = vertex;
backVertex = vertex;

sets the pointer value but not the actual value. You malloc space, get a pointer to that space, and then throw that pointer away setting it to the pointer of virtex.

Do you mean to use

*leftVertex = *vertex;
*rightVertex = *vertex;
*upwardVertex = *vertex;
*downwardVertex = *vertex;
*frontVertex = *vertex;
*backVertex = *vertex;
///Scrolling the list
while(vertex->prev != NULL){
    vertex = vertex->prev;

And what happens if vertex is NULL after this?

You're checking if it's NULL, then changing it's value such that it can become NULL.

  ///Opening file
if(modelInfoFile == NULL){
    printf("Error in file opening. Exiting.");
    exit(EXIT_FAILURE);
}

I don't see you opening file.

if((modelInfoFile=fopen(filename,"w")) == NULL){

Should work.

EDIT

In you while loop you change -

   vertex = vertex->prev;

But in fprintf you store in file in value of leftVertex->vertexCoordinates.x So how do you expect to print inside file correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM