繁体   English   中英

C编程-cicle不起作用时的fprintf和printf

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

我在函数内部使用cicle时遇到了一个奇怪的问题。

我必须寻找.ply模型的极端顶点。 所有数据都存储在链接列表中。 创建完列表后,我将调用findExtremeVertex函数,该函数修改6个全局变量(leftVertex,rightVertex,downwardVertex,upwardVertex,backVertex和frontVertex)。

为了查看值是否正确(我使用的模型太大了,无法控制每一行以找到每个顶点的最大值),我决定打印max-min值中的每个更改,但是,当我尝试打印它们时在文件中,该文件为空。 这是为什么? 另外,当我看到文件为空时,我尝试直接在控制台中打印某些内容,但这也不起作用。

这是该功能的代码:

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

我在另一个函数中调用了此函数,但是没有分段错误信号,编译器什么也没告诉我,程序也不会崩溃。 我不知道该错误的线索,除了我打印有关循环的信息的文件为空的事实。 我究竟做错了什么?

您的代码中有很多问题。

  1. 您使用malloc() 6个变量,并且从不使用任何变量,并且您不检查malloc()成功。

  2. 您永远不会调用fclose()fflush()因此也许在将数据刷新到磁盘之前就已经看到了文件。

  3. 重新分配所有的*Vertex (除rightVertex )变量后,他们malloc()版相同的指针vertex该装置

    1. 您正在导致内存泄漏。
    2. 您将6个变量用于单个指针。
  4. 没有在函数内部声明所有*Vertex变量,这意味着它们在全局范围内,这很可能是错误的设计选择。 给定您发布的代码,就无法确定全局变量是否是正确的选择,但是有99%的时间它们是一个错误的选择,并且有一种更优雅,更安全的处理方法。

上面的粗体点可能是程序按原样运行的原因。

编码

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

设置指针值,但不设置实际值。 您分配空间,获取指向该空间的指针,然后将其扔掉,将其设置为virtex指针。

你是不是要用

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

如果此后vertex为NULL,会发生什么?

您正在检查它是否为NULL,然后更改其值以使其变为NULL。

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

我看不到您打开文件。

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

应该管用。

编辑

在您的while循环中,您进行更改-

   vertex = vertex->prev;

但是在fprintf您以leftVertex->vertexCoordinates.x值存储在文件中。因此,您如何期望正确打印文件内部。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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