C编程- fprintf和printf in, cicle不工作。

时间:2023-02-09 20:42:59

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

我必须寻找一个。ply模型的极端顶点。所有数据都存储在一个链表中。当我创建列表时,我调用findextreme顶点函数,它修改了6个全局变量(左顶点、右顶点、向下顶点、向上顶点、后顶点和前顶点)。

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:

下面是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?

我在另一个函数中调用这个函数,但是没有分割错误信号,编译器没有告诉我任何东西,程序不会崩溃。我不知道这个错误,除了我打印信息的文件是空的。我做错了什么?

4 个解决方案

#1


1  

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.

    malloc() 6变量,且不使用任何变量,也不检查malloc()是否成功。

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

    您永远不会调用fclose()或fflush(),所以在数据被刷新到磁盘之前,您可能已经看到了该文件。

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

    你重新分配了所有的*顶点(除了右顶点),因为它们是malloc()到相同的指针顶点。

    1. You are causing a memory leak.
    2. 您正在造成内存泄漏。
    3. You are using 6 variables for a single pointer.
    4. 您使用6个变量作为一个指针。
  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.

    所有的顶点变量都没有在函数中声明,这意味着它们在全局范围内,这很可能是一个糟糕的设计选择。考虑到你发布的代码,不可能判断全局变量是否是正确的选择,但是99%的情况下,它们是一个糟糕的选择,而且有一个更加优雅和安全的方法来做事情。

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

上面的黑点很可能是您的程序运行正常的原因。

#2


1  

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.

设置指针值,但不设置实际值。malloc空间,得到一个指向该空间的指针,然后将指针移开,将其设置为virtex的指针。

Do you mean to use

你打算用吗?

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

#3


1  

///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.

你检查它是否为空,然后改变它的值,这样它就可以变成空值。

#4


0  

  ///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 -

在你while循环中你改变。

   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.

但是在fprintf中,你存储的文件的值是leftVertex->的顶点坐标。那么如何正确地打印内部文件呢?

#1


1  

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.

    malloc() 6变量,且不使用任何变量,也不检查malloc()是否成功。

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

    您永远不会调用fclose()或fflush(),所以在数据被刷新到磁盘之前,您可能已经看到了该文件。

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

    你重新分配了所有的*顶点(除了右顶点),因为它们是malloc()到相同的指针顶点。

    1. You are causing a memory leak.
    2. 您正在造成内存泄漏。
    3. You are using 6 variables for a single pointer.
    4. 您使用6个变量作为一个指针。
  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.

    所有的顶点变量都没有在函数中声明,这意味着它们在全局范围内,这很可能是一个糟糕的设计选择。考虑到你发布的代码,不可能判断全局变量是否是正确的选择,但是99%的情况下,它们是一个糟糕的选择,而且有一个更加优雅和安全的方法来做事情。

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

上面的黑点很可能是您的程序运行正常的原因。

#2


1  

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.

设置指针值,但不设置实际值。malloc空间,得到一个指向该空间的指针,然后将指针移开,将其设置为virtex的指针。

Do you mean to use

你打算用吗?

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

#3


1  

///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.

你检查它是否为空,然后改变它的值,这样它就可以变成空值。

#4


0  

  ///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 -

在你while循环中你改变。

   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.

但是在fprintf中,你存储的文件的值是leftVertex->的顶点坐标。那么如何正确地打印内部文件呢?