double Writing to and reading from binary file

Hello People!

I am trying to write and read double to and from binary files.

I am using the following sentences in order to do that.

stm.write(reinterpret_cast<const char*>(&auxiliary), sizeof(auxiliary));

stm.read(reinterpret_cast<char*>(&read), sizeof(read));

where stm is a stringstream.

The problem is that I am losing precision when I write the stream to a file and then I load another stream from the same file.

For example:

I write the following numbers in the stream:
0 3.623e-07 3.623e-07 0.00219843 0.016128 0 0 0 0 0 4.83066e-07 0 0 0 0 6.03833e-08 0 0 0 0 0.0901358 0.00586247 0.000367129 2.85009e-05 0 0 0 0.0410373 0.0117251 8.81596e-06 2.16776e-05 0 0 0 5.91756e-06

I save the stream in a file,

I read such file and load its contents in another stream.

Then, when I want to load the doubles from this stream, I get:
0 0 0 0.00219843 0.0161218 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0901355 0.00586247 0.000366405 0 0 0 0 0.0410373 0.0117249 0 0 0 0 0 0.

Certain number as 3.623e-07 is loaded as 0.

What could be the problem?

Thanks


Last edited on
Giving a written description of your code is not quite so useful as actually posting the code itself. The problem is almost certainly in the code you haven't shown.

Here, I just use a file, not a stringstream, but tried to use your code as written where possible (about two lines here).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    double auxiliary[] = {
        0, 3.623e-07, 3.623e-07, 0.00219843, 0.016128,
        0, 0, 0, 0, 0,
        4.83066e-07, 0, 0, 0, 0,
        6.03833e-08, 0, 0, 0, 0,
        0.0901358, 0.00586247, 0.000367129, 2.85009e-05, 0,
        0, 0, 0.0410373, 0.0117251, 8.81596e-06,
        2.16776e-05, 0, 0, 0, 5.91756e-06 };
        
    int i = 0;    
    for (auto a : auxiliary)
    {
        if (i > 0 && i% 5 == 0) cout << '\n';
        ++i;
        cout << setw(15) << a;
    }

    {
        ofstream stm("numbers.bin", ios::binary);    
        stm.write(reinterpret_cast<const char*>(&auxiliary), sizeof(auxiliary));
    }
    
    {
        cout << "\n\nReading:\n";
        
        ifstream stm("numbers.bin", ios::binary);    
    
        int i = 0;    

        for (double read; stm.read(reinterpret_cast<char*>(&read), sizeof(read)); )
        {
            if ( auxiliary[i] != read) cout << "different\n";
            
            if (i > 0 && i% 5 == 0) cout << '\n';
            ++i;            
            cout << setw(15) << read;            
        }
    }
      
}


Thanks Chervil,

you are right, the problem seemed to be in a MPI I/O library I was using.

I didn't post the code because it is huge and there is MPI code which does not correspond to a C++ forum. Yet your help was great, because I put that possibility out of the question.

Thanks!
Topic archived. No new replies allowed.