c - Problem writing array of doubles to a file -


I have a very large array of double exact numbers ... I tried to write fprintf () in a file ... I have to write one of these numbers in each row, so I did something like that.

  if ((fp2 = fopen ("temp", "w") == faucet) {false ("file can not be opened"); Exit (1); } For (int k = 0; k & lt; j; k ++) {fprintf (fp2, "% 0.3lf \ n", diff [k]); }  

However, one problem is that he writes data to a certain number after which I give all the zeros. For example

  3.040 0.700 -2.740 0.000 0.000 0.000 0.000 0.000 0.000  

I really can not understand what the problem might be. Why is all values ​​written as 0.000 when there are values ​​in the array.

How does this differ here if it helps?

  diff = (double *) malloc (file size); Diff [0] = data [0]; For (j = 1; j; l & lt; n; j ++) {diff [j] = data [j] - data [j-1]; }  

Valid data stored from a file [] Then I calculated the differences of the adjacent values ​​in the data [] diff [] in the file and it was written back in the second file FileSize Basic File size was. And I know to make sure all the values ​​in diff [] are populated correctly.

Answer an official comments:

You have the following line:

/ P>

  diff = (double *) malloc (fileSize);  

Note that when you are allocating files in the malloc (fileSize) file byte each double takes 8 bytes in memory (Depends on more or less platforms) so that you have file size / 8 elements in your array. Do you make sure that most of the elements in your array are never n?

Your program may be Maybe printf is the reason for working correctly FprintF is that the memory bug It is very subtle if you start using memory, then you have not allocated, then things can be cured for a short time, but in the second part of the program decided to start the piece of memory quite legally Goes, which you are already using May include.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -