Hey guys, I'm working on a piece of code that examines a *.stl ASCII file generated by CAD that's a list of vertices for a 3D model. My code plods through the file with fscanf, gets the coordinates of the vertices, and computes a bunch of maxima and minima. The program compiled and ran fine with this open-source compiler that I have (BloodShed DevC++ 4), but when I switched to MSVC++ .NET 2003, I started getting this error after the program was finished running:

Additionally, some of the computed values had become obviously wrong.Run-Time Check Failure #2 - Stack around the variable 'normal' was corrupted.

The *.stl file is a list of vertices that looks like this:

And here's my code:solid exth

facet normal 5.612239e-01 6.904722e-01 -4.563725e-01

outer loop

vertex 3.362700e-01 8.505000e-01 9.112400e-02

vertex 3.457880e-01 8.414510e-01 8.913800e-02

vertex 3.241640e-01 8.400320e-01 6.039900e-02

endloop

endfacet

facet normal -8.197013e-01 5.350310e-01 2.045279e-01

outer loop

vertex 2.414680e-01 8.004080e-01 1.561220e-01

vertex 2.330830e-01 7.962320e-01 1.334410e-01

vertex 2.282310e-01 7.818640e-01 1.515810e-01

endloop

endfacet

...

facet normal 3.132712e-01 -7.011380e-01 -6.405206e-01

outer loop

vertex 1.383960e-01 3.755810e-01 1.190080e-01

vertex 1.139660e-01 3.675140e-01 1.158900e-01

vertex 1.133830e-01 3.804040e-01 1.014950e-01

endloop

endfacet

endsolid

Now, I acknowledge that my use of fscanf is lazy, inefficient, and clumsy, but I'm fairly certain it shouldn't be causing these problems that I'm having.Code:#include <stdlib.h> #include <stdio.h> int main(void) { //Define variables for file input - the FILE variables designate the input and //output files, the xyzs are for coordinates, and the rest are junk data //placeholders for fscanf. char input_path[261]; float x1, x2, x3, y1, y2, y3, z1, z2, z3, junk1, junk2, junk3; char facet[6], normal[7], outer[6], loop[5], vertex1[7], vertex2[7], vertex3[7], endloop[8], endfacet[9]; char solid[6], exth[5]; FILE *input, *output; //Define varibales for data manipulation. float x_max, y_max, z_max, x_min, y_min, z_min; float x_dim, y_dim, z_dim; float d_12, d_23, d_13; float d_max, d_min, d_avg; float sum; int counter; //Prompts the user to enter the input file path. puts("Enter the input file path: "); gets(input_path); //Open input file. Return error message if failed. if((input = fopen(input_path, "r")) == NULL) { fprintf(stderr, "Error opening input file.\n"); system("PAUSE"); exit(1); } //Read those first two strings. fscanf(input, "%s %s", solid, exth); //Display a friendly message while the program works. puts("Computing, please wait..."); //Read coordinates for first element, store them in xyzs. fscanf(input, "%s %s %f %f %f %s %s %s %f %f %f %s %f %f %f %s %f %f %f %s %s", facet, normal, &junk1, &junk2, &junk3, outer, loop, vertex1, &x1, &y1, &z1, vertex2, &x2, &y2, &z2, vertex3, &x3, &y3, &z3, endloop, endfacet); //Compute distances. d_12 = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2)); d_23 = sqrt((x2-x3)*(x2-x3) + (y2-y3)*(y2-y3) + (z2-z3)*(z2-z3)); d_13 = sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3) + (z1-z3)*(z1-z3)); //Initialize d_max, d_min, and max and min xyzs. d_max = d_12; d_min = d_12; x_max = x1; x_min = x1; y_max = y1; y_min = y1; z_max = z1; z_min = z1; //Find d_max and d_min for the first time. if(d_23 > d_max) d_max = d_23; if(d_13 > d_max) d_max = d_13; if(d_23 < d_min) d_min = d_23; if(d_13 < d_min) d_min = d_13; //Find max and min xyzs for the first time. if(x2 > x_max) x_max = x2; if(x3 > x_max) x_max = x3; if(x2 < x_min) x_min = x2; if(x3 < x_min) x_min = x3; if(y2 > y_max) y_max = y2; if(y3 > y_max) y_max = y3; if(y2 < y_min) y_min = y2; if(y3 < y_min) y_min = y3; if(z2 > z_max) z_max = z2; if(z3 > z_max) z_max = z3; if(z2 < z_min) z_min = z2; if(z3 < z_min) z_min = z3; //Initialize sum. sum = d_12 + d_23 + d_13; //Initialize counter. counter = 1; //After the first element, for which all the variables had to be initialized, //this while loop can do all the work. while( !feof(input) ) { //Read coordinates for an element and store them in the xyzs. fscanf(input, "%s %s %f %f %f %s %s %s %f %f %f %s %f %f %f %s %f %f %f %s %s", facet, normal, &junk1, &junk2, &junk3, outer, loop, vertex1, &x1, &y1, &z1, vertex2, &x2, &y2, &z2, vertex3, &x3, &y3, &z3, endloop, endfacet); //Compute distances. d_12 = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2)); d_23 = sqrt((x2-x3)*(x2-x3) + (y2-y3)*(y2-y3) + (z2-z3)*(z2-z3)); d_13 = sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3) + (z1-z3)*(z1-z3)); //Find d_max and d_min. if(d_12 > d_max) d_max = d_12; if(d_23 > d_max) d_max = d_23; if(d_13 > d_max) d_max = d_13; if(d_12 < d_min) d_min = d_12; if(d_23 < d_min) d_min = d_23; if(d_13 < d_min) d_min = d_13; //Find max and min xyzs. if(x1 > x_max) x_max = x1; if(x2 > x_max) x_max = x2; if(x3 > x_max) x_max = x3; if(x1 < x_min) x_min = x1; if(x2 < x_min) x_min = x2; if(x3 < x_min) x_min = x3; if(y1 > y_max) y_max = y1; if(y2 > y_max) y_max = y2; if(y3 > y_max) y_max = y3; if(y1 < y_min) y_min = y1; if(y2 < y_min) y_min = y2; if(y3 < y_min) y_min = y3; if(z1 > z_max) z_max = z1; if(z2 > z_max) z_max = z2; if(z3 > z_max) z_max = z3; if(z1 < z_min) z_min = z1; if(z2 < z_min) z_min = z2; if(z3 < z_min) z_min = z3; //Revise sum. sum += d_12 + d_23 + d_13; //Increment counter. counter++; } //Close input file. fclose(input); //Calculate average distance. d_avg = sum / counter; //Calculate x_dim, y_dim, and z_dim. x_dim = x_max - x_min; y_dim = y_max - y_min; z_dim = z_max - z_min; //Output information to the output file. printf("\nINFORMATION FOR FILE %s\n", input_path); printf("---\n\n"); printf("There are %d elements.\n\n", counter); printf("The maximum distance is %f .\n", d_max); printf("The minimum distance is %f .\n\n", d_min); printf("The average distance is %f .\n\n", d_avg); printf("The x-dimension size is %f .\n", x_dim); printf("The y-dimension size is %f .\n", y_dim); printf("The z-dimension size is %f .\n\n", z_dim); printf("---\n"); printf("END\n\n"); system("PAUSE"); return 0; }

Anyone have any ideas for a fix?

(EDITED so as not to break the forum form.)