0

I used to print data from a C code in a text file as,

fp = fopen("MD.txt", "a+");
//saving iteratively
fprintf(fp,"%f %f %f \n", x, y, z);

This way the data saved in the text file becomes huge (in several GBs) as I am saving positions from a molecular dynamics code at each time step. I wanted to know If there is any other file format which can save the same amount of data but in a smaller file.

yasir
  • 123
  • 8
  • Maybe a duplicate: [Reand and write to binary files in C](https://stackoverflow.com/q/17598572/10553341) – Damien Dec 10 '20 at 09:30

2 Answers2

0

Why not save the values as binary? (Also more accurate)

i.e to write fwite:

float x = 123.45;
fwrite(&x, sizeof(float), 1, fh) // and check the return value

To read fread

fread(&x, sizeof(float), 1, fh)  // and check the return value

This should save a lot of space

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • can I write data with binary format in a Matrix form like 2 3 4\n 6 7 8\n, or only Rows possible, meaning each line with 1 data only? – yasir Dec 10 '20 at 09:47
0

You can save this data as binary data, it will not be human readable but it should take less space. For typical architecture each float should take only 4 bytes.

Code example:

fp = fopen("data.bin", "wb");

if(fp == NULL)
{
    printf("Error opening file\n");
    exit(1);
}
for (...) {
    float x, y, z;
    fwrite( &x, 1, sizeof(x), fp );
    fwrite( &y, 1, sizeof(y), fp ) ;
    fwrite( &z, 1, sizeof(z), fp ) ;
}

Read can be done using http://www.cplusplus.com/reference/cstdio/fread/

size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );

This way you will get rid of spaces, new line sign and each float will take less space. For example float 132.002 takes 7 bytes and binary form will always take 4 bytes.

Only downside is that there are no delimiters between each float so read function has to assume how data was written to file.

trebuhg
  • 16
  • 1