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;
}
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.