1

I want to make a small change in one C code,in order to double-check some results.Just few relevant lines

 FILE           *f1_out, *f2_out;

/* open files */
if ((f1_out = fopen(vfname, "w")) == (FILE *) NULL)
{
   fprintf(stderr, "%s: Can't open file %s.\n", progname, vfname);
   return (-1);
}

Then goes some calculations and

yes = fwrite(vel, nxyz*sizeof(float), 1, f1_out);

How to change the last line to get the ascii output?

Richard Rublev
  • 7,718
  • 16
  • 77
  • 121

3 Answers3

2

Assuming that your vel is an array of floats, and your nxyz is the number of floats in that array, and that you want the output on the standard output and not on the file you opened:

for (int i = 0; i < nxyz; ++i) {
    printf("vel[%d] = %f\n", i, vel[i]);
}
Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75
1

Instead of yes = fwrite(vel, nxyz*sizeof(float), 1, f1_out);, use

for(i = 0; i < nxyz; ++i) {
  yes &= (fprintf(f1_out, "%d:\t%f\n", i, vel[i]) > 0);
}
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
1

To see all meaningful digits in a float as text use printf("%*e", some_precision, some_float)

#include <float.h>
fprintf(f1_out, "%.*e\n", FLT_DECIMAL_DIG - 1, *vel);
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256