Search code examples
c++fwritefreadmingw-w64

Why do fwrite / fread not like 56.806201550387598?


I am trying to save a struct to a file and then read it again in another program. I never had problems with this before, but at least to my knowledge, I never tried writing a double variable with the value 56.806201550387598 to a file. Not that I have any idea why this number should make any problems whatsoever but whenever I try to write a struct containing this value to a file, that field and any field declared after it, are only able to read out the initialization values from the struct declaration.

#include <fstream>
#include <iostream>
#include <string> 

using namespace std;

typedef struct statsSet{
    int pathCount = 0;
    double vMinMean = 0;
    int imgsFound = 0;
    int minWidth = INT_MAX;
    int maxWidth = 0; 
}statsSet;

void wrightToFile(statsSet sSet){
    FILE* of = fopen ("imgStats2.dat", "w"); 
    fwrite (&sSet, sizeof(statsSet), 1, of); 
    fclose (of);
}
statsSet readFromFile(){
    statsSet statReadIn;
    FILE *ifl = fopen ("imgStats2.dat" , "r");
    fread(&statReadIn, sizeof(statsSet), 1, ifl);
    fclose (ifl);
    return statReadIn;
}
void pr(statsSet statReadIn){   
    cout << "pathCount: " << statReadIn.pathCount << "\n";
    cout << "vMinMean: " << statReadIn.vMinMean<< "\n";
    cout << "imgsFound: " << statReadIn.imgsFound<< "\n";
    cout << "minWidth: " << statReadIn.minWidth<< "\n";
    cout << "maxWidth: " << statReadIn.maxWidth<< "\n";
    cout << "--------\n";
}

int main( int argc, char** argv ){
    statsSet statTest; 
    statsSet statReadIn;

    statTest.pathCount = 129;
    statTest.imgsFound = 129;
    statTest.minWidth = 488;
    statTest.maxWidth = 501;

    statTest.vMinMean = 56.806201550387591; // OK
    wrightToFile(statTest);
    statReadIn = readFromFile();
    pr(statReadIn);


    statTest.vMinMean = 56.806201550387598;   //breaks
    wrightToFile(statTest);
    statReadIn = readFromFile();
    pr(statReadIn);

    
    statTest.vMinMean = 56.806201550387605; // OK
    wrightToFile(statTest);
    statReadIn = readFromFile();
    pr(statReadIn);
}

output:

pathCount: 129
vMinMean: 56.8062
imgsFound: 129
minWidth: 488
maxWidth: 501
--------
pathCount: 129
vMinMean: 0
imgsFound: 0
minWidth: 2147483647
maxWidth: 0
--------
pathCount: 129
vMinMean: 56.8062
imgsFound: 129
minWidth: 488
maxWidth: 501
--------

What am I doing wrong?

I'm using Windows 10, Visual Studio Code, and MinGW-w64.


Solution

  • Use Write Binary mode. Text mode will be very random for binary data.

    FILE* of = fopen ("imgStats2.dat", "wb"); 
    

    and

    FILE *ifl = fopen ("imgStats2.dat" , "rb");