SamGamgee
SamGamgee

Reputation: 564

convert doubles from a text file into a binary file

I have a text file full of numbers (from a molecular dynamics output, should be type double) divided in three columns and with thousands of lines like this:

    -11.979920  -13.987064   -0.608777
     -9.174895  -13.979109   -0.809622

I want to read all the numbers in the file, convert them to type double and then save them into a binary file. I know that binary file is not recommended, but I want it to test compression algorithms with both file formats, i.e text and binary. I have tried with Need to convert txt file into binary file in C++ but I am not sure if is converting each number in its whole format: -11.275804 or if it is parsing each individual number: -1,1,2,7,5, etc..

Edit: Have been trying to convert a single double to binary and back, there are some problems. Here's the core code

    if( std::string(argv[1]) == "-2bin")  //convert to binary
{
    cout << "Performing the conversion: Text -> Binary..." << endl;
    std::ifstream in(argv[2]);
    std::ofstream out(argv[3], std::ios::binary);
    double d;

    while(in >> d) {
        cout << "read a double: "<< d <<endl;
        out.write((char*)&d, sizeof d);
    }
    out.flush();
    out.close();

    cout << "Conversion complete!" << endl;
    return 0;
}else if( std::string(argv[1]) == "-2text" ) //convert to text
{
    cout << "Performing the conversion: Binary -> Text..." << endl;
    std::ifstream in(argv[2], std::ios::binary);
    std::ofstream  out(argv[3]);
    std::string s;

    while(in >> s) {
        cout << "read a string:" << s <<endl;
        out.write((char*)&s, s.length());
    }
    out.flush();
    out.close();

    cout << "Conversion complete!" << endl;
    return 0;

When reading only one double, for example 1.23456789 the read string is just of length 7

read a double: 1.23457

I want it to read until the next 'space' and then do the conversion to double->bin. When doing the binary -> text conversion everything is just broken, I don't know how to handle the binary and convert it to double and then string.

Update: Finally I managed to check that the binary conversion is working with od -lF utility, but every each line there is a strange line that I have no idea what it means, it cuts the zero from the first number and the output comes in two columns instead of 3:

od -lF composite_of10_2.bin | more
0000000     -4600438323394026364     -4599308401772716498
                       -11.97992               -13.987064
0000020     -4619713441568795935     -4602017412087121980
                       -0.608777                -9.174895
0000040     -4599312880039595965     -4617904390634477481
                      -13.979109                -0.809622

Does this look correctly converted? How should I perform the conversion from binary to double string?

Upvotes: 1

Views: 1024

Answers (1)

Crog
Crog

Reputation: 1170

In your binary -> text conversion you read in the data as a 'string' while your binary file contains binary 'doubles'

std::string s;

    while(in >> s) { ...

This is going to give undefined results, you need to read in as 'double' and convert the value into a string, this will be handled natively by the text output stream

double d;
while( in.read( (char*)&d, sizeof(d) ) { //, Read the double value form the binary stream
    out << d << ' '; //< Write the double value to the output stream which is in text format
}

Upvotes: 2

Related Questions