4

I want to perform disk I/O operations for a program that takes too much RAM. I use matrices of doubles and think writing them to disk as bytes is the fastest way (I need to preserve the double precision).

How to do it with portability?

I found this code (here) but the author says it's not portable...

#include <iostream>
#include <fstream>

int main()
{
    using namespace std;

    ofstream ofs( "atest.txt", ios::binary );

    if ( ofs ) {
        double pi = 3.14;

        ofs.write( reinterpret_cast<char*>( &pi ), sizeof pi );
        // Close the file to unlock it
        ofs.close();

        // Use a new object so we don't have to worry
        // about error states in the old object
        ifstream ifs( "atest.txt", ios::binary );
        double read;

        if ( ifs ) {
            ifs.read( reinterpret_cast<char*>( &read ), sizeof read );
            cout << read << '\n';
        }
    }

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Andy
  • 429
  • 1
  • 6
  • 17
  • 2
    It's not portable because `sizeof(double)` might be different across platforms. You'll need to check this but otherwise the sample code is ok. – spraff Jul 22 '11 at 12:15
  • 1
    I'd be more worried about the byte order of `double` because that's more likely to change (especially if interacting with any embedded hardware). – Mark B Jul 22 '11 at 12:25
  • True, although to talk and think in terms of "likely" is a bad habit. If you really care about portability, there are no degrees of it. – spraff Jul 22 '11 at 12:29

3 Answers3

5

How to do it with portability?

There are different definitions/levels of portability. If all you ever do is to write these on one machine and read it on the same one, the only portability you are concerned with is whether this code is well-defined. (It is.)
If you want to write portably across several different platforms, you need to write string values, rather than binary ones.

However, note that the code you have lacks proper error handling. It doesn't check whether the file could be opened and successfully written to.

sbi
  • 219,715
  • 46
  • 258
  • 445
  • @Luchian: I am very sorry. `:)` – sbi Jul 22 '11 at 12:31
  • my "level" of portability is from personnal computers (on windows and linux/unix) to clusters. And i want to write bytes because i need maximum speed and minimum size – Andy Jul 22 '11 at 12:36
  • @Andy: You misunderstood. Do you want to write those files on a Windows machine and read them on a Unix machine? Or do you want to write and read them on the same machine (but the code should work on any machine)? – sbi Jul 22 '11 at 12:44
  • sorry :) the I/O would be on the same machine. Would the boost serialization library be a good way to do that ? – Andy Jul 22 '11 at 12:53
  • @Andy: I haven't worked with it, but I suppose it writes/reads stringized data. If that's strictly and only on the same machine, writing binary should indeed be faster. And it should work on any (one) platform. – sbi Jul 22 '11 at 16:13
2

I think that the portability issue only occurs when you write to a file and read from it on a different machine. However, since you said you want to read/write to a file because of ram limitations, I can only assume you would do read/write operations on one machine at a time. This should work.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

He may have been referring to binary representation in general, not just sizeof.

C - Serialization of the floating point numbers (floats, doubles)

The spec doesn't specify binary representation of floating point numbers at all. Most compilers follow IEEE, so a little unit testing should ensure the behavior you want if you know your target platforms.

Community
  • 1
  • 1
Tom Kerr
  • 10,444
  • 2
  • 30
  • 46
  • 2
    But don't forget that some machines use little-endian IEEE and some use big-endian IEEE formats - roughly Intel and 'everyone else' (PPC, SPARC, ...). – Jonathan Leffler Jul 22 '11 at 15:32
  • Yes, definitely. I think the point is that you need to write tests to ensure validity on every platform you wish to target. – Tom Kerr Jul 22 '11 at 15:43