I’m a beginner to C but not too bad at programming all around. I’m writing a program in C that calculates the path of an object (x, y, and z coordinates) according to few equations which depend on time. I then want to print these coordinates to an output file so I can plot them. Right now, I have:
double *t;
double *x;
double *y;
double *z;
I do my calculations and fill up x, y, and z. I then try to print them to a file:
FILE *outputFile = fopen("path.out", "w");
for (i = 0; i<(sizeof(x)/sizeof(double)); ++i)
char *strX = (char *) *(x+i);
char *strY = (char *) *(y+i);
char *strZ = (char *) *(z+i);
fprintf(*outputFile, *strX);
fprintf(*outputFile, "\t");
fprintf(*outputFile, *strY);
fprintf(*outputFile, "\t");
fprintf(*outputFile, *strZ);
I’m getting a whole mess of errors trying to cast each double to a char *, but I’m not sure why. Is there a better way to print the doubles or to cast them to a char * for printing?
You are using “fprintf” which requires a format specification.
There is no need for pointer casting etc. as this is all handled by the fprintf function according to the format supplied so to print out three doubles separated by tabs you only need:
More details on what you can put in the format string here:
tutorial