开发者

Print Numbers to File in C?

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 *) 开发者_StackOverflow中文版*(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:

fprintf(outputFIle,"%lf\t%lf\t%lf",x + i, y + i, z + i);

More details on what you can put in the format string here: tutorial


I notice that t,x,y,z are of type double*. Is there a reason why these need to be pointers? Probably not. Unless you have a very good reason, you shouldn't be using pointers in this code. (FILE* is okay because that's the return type of fopen.)

Also, you can't just cast a double to a char* and expect it to be written to the file as a string. fprintf() lets you give it a formatted string to output. For example: fprintf(outputFile, "%f", x); will print a double to the output. Also notice that the 1st param to fprintf is of type FILE*, so you don't need to dereference it.


double *t;
double *x;
double *y; 
double *z;

As Erik said, there is no need to make those pointers, unless you want to make them point to other variables or want to malloc or calloc them. You can simply do:

double t;
double x;
double y; 
double z;

But this is also a little weird:

for (i = 0; i<(sizeof(x)/sizeof(double)); ++i)

Where did you see that? It is sometimes used to get the number of items in an array:

len = sizeof(myArray)/sizeof(myArray[0]);

but x is not an array, in your code it is a pointer, and probably smaller than a double, so the result of the division is 0 and chances are high the loop will not run at all. Assuming you meant the entire indented code to be looped over, you also forgot the { and } to surround the block, so the loop will only perform the first line of that "block".

Then you try to cast a double into a string (hence the warnings), but that is not how it works, as it will merely try to reinterpret the value in the double as a pointer to a char, which is certainly not what you want, and it is unnecessary, since fprintf will take care of that for you:

fprintf(outputFile, "%f\t%f\t%f\n", x, y, z);

The loop and the fact you are using pointers make me think you are trying to loop over something, but it is not entirely clear what you actually want to achieve. Could you explain what you actually want to do?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜