Это мой код в VS 2019. Он застрял на fclose(fp);
строке с сообщением:
projectname.exe вызвал точку останова.
Без отладки не показывает «законченное чтение» в консоли. Тем не менее, пару раз раньше это делает. Я думаю, что проблема как-то связана с выделением памяти, потому что иногда она останавливается даже до fclose(fp)
строки.
Два раза он также создал файл "SmallSphere02_out.txt" и записал все правильно. Однако это не соответствует! Есть идеи?
#pragma warning(disable:4996) // disabling fopen and fscanf unsafe error [These are only MS required things not c++]
#include <stdio.h>
#include <stdlib.h>
FILE* fp;
FILE* fc;
struct vertices {
float x;
float y;
float z;
} verices1;
struct polygons {
int n1;
int n2;
int n3;
} plygons1;
struct polyhedron {
int numVertices;
int numPolygons;
//struct vertices vertices1;
//struct polygons plygons1;
}polyhedron1;
void main()
{
vertices* pv = (vertices*)malloc(polyhedron1.numVertices * sizeof(vertices));
//printf("%p\n", pv);
if (pv==NULL)
{
printf("Null pointer error of pv");
}
polygons* pp = (polygons*)malloc(polyhedron1.numPolygons * sizeof(polygons)+1000);
if (pp == NULL)
{
printf("Null pointer error of pp");
}
// used for checking before pointers
//float a, b, c;
//int x, y, z;
// Reading
fp = fopen("SmallSphere02.txt", "r");
fscanf(fp,"%d\n", &polyhedron1.numVertices); //66
printf("%d\n", polyhedron1.numVertices);
for (int i = 0; i < polyhedron1.numVertices; i++) // 66 vertices coordinates
{
fscanf(fp, "%f\t%f\t%f\n", &pv[i].x, &pv[i].y, &pv[i].z);
printf("%f\t%f\t%f\n", pv[i].x, pv[i].y, pv[i].z);
}
fscanf(fp, "%d\n", &polyhedron1.numPolygons); //128
printf("%d\n", polyhedron1.numPolygons);
for (int i = 0; i < polyhedron1.numPolygons; i++) //128 polygons connectivity info
{
fscanf(fp, "%d\t%d\t%d\n", &pp[i].n1, &pp[i].n2, &pp[i].n3);
printf("%d\t%d\t%d\n", pp[i].n1, pp[i].n2, pp[i].n3);
}
fclose(fp);
printf("Finished_Reading");
//Writing
fc = fopen("SmallSphere02_out.txt", "w");
fprintf(fc, "%d\n", polyhedron1.numVertices); //66
for (int i = 0; i < polyhedron1.numVertices; i++) // 66 vertices coordinates
{
fprintf(fc, "%f\t%f\t%f\n", pv[i].x, & pv[i].y, pv[i].z);
}
fprintf(fc, "%d\n", polyhedron1.numPolygons); //128
for (int i = 0; i < polyhedron1.numPolygons; i++) //128 polygons connectivity info
{
fprintf(fc, "%d\t%d\t%d\n", pp[i].n1, pp[i].n2, pp[i].n3);
}
fclose(fc);
printf("Finished_Writing"); // Checking Error
//free(fp);
free(pp);
free(pv);
printf("Finished_End"); // Checking Error
}