[英]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
**/
}
}
我在另一個函數中調用了此函數,但是沒有分段錯誤信號,編譯器什么也沒告訴我,程序也不會崩潰。 我不知道該錯誤的線索,除了我打印有關循環的信息的文件為空的事實。 我究竟做錯了什么?
您的代碼中有很多問題。
您使用malloc()
6個變量,並且從不使用任何變量,並且您不檢查malloc()
成功。
您永遠不會調用fclose()
或fflush()
因此也許在將數據刷新到磁盤之前就已經看到了文件。
重新分配所有的*Vertex
(除rightVertex
)變量后,他們malloc()
版相同的指針vertex
該裝置
沒有在函數內部聲明所有*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.