Binary files printing and desired precision

Posted by yCalleecharan on Stack Overflow See other posts from Stack Overflow or by yCalleecharan
Published on 2010-04-05T22:53:32Z Indexed on 2010/04/06 10:23 UTC
Read the original article Hit count: 301

Filed under:
|
|
|
|

Hi, I'm printing a variable say z1 which is a 1-D array containing floating point numbers to a text file so that I can import into Matlab or GNUPlot for plotting. I've heard that binary files (.dat) are smaller than .txt files. The definition that I currently use for printing to a .txt file is:

void create_out_file(const char *file_name, const long double *z1, size_t z_size){
FILE *out;
size_t i;
 if((out = _fsopen(file_name, "w+", _SH_DENYWR)) == NULL){
 fprintf(stderr, "***> Open error on output file %s", file_name);
 exit(-1);
 }
for(i = 0; i < z_size; i++)
fprintf(out, "%.16Le\n", z1[i]);
fclose(out);
}   

I have three questions:

  1. Are binary files really more compact than text files?;

  2. If yes, I would like to know how to modify the above code so that I can print the values of the array z1 to a binary file. I've read that fprintf has to be replaced with fwrite. My output file say dodo.dat should contain the values of array z1 with one floating number per line.

  3. I have %.16Le up in my code but I think that %.15Le is right as I have 15 precision digits with long double. I have put a dot (.) in the width position as I believe that this allows expansion to an arbitrary field to hold the desired number. Am I right? As an example with %.16Le, I can have an output like 1.0047914240730432e-002 which gives me 16 precision digits and the width of the field has the right width to display the number correctly. Is placing a dot (.) in the width position instead of a width value a good practice?

Thanks a lot...

© Stack Overflow or respective owner

Related posts about binary

Related posts about files