Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9202571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:17:18+00:00 2026-06-17T23:17:18+00:00

I have a text file full of numbers (from a molecular dynamics output, should

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:17:19+00:00Added an answer on June 17, 2026 at 11:17 pm

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file full of numbers and I want to read the
I have a text file full of records (output.txt) and I want to sort
I have a text file of full of sentences and I am trying to
I have text file with something like first line line nr 2 line three
I want to read in data from a text file which is full of
I have text file full of following lines: F randomtext F morerandomtext what kind
I have a text file full of words. I need to go through the
I have a text file full of stopwords. I would like to represent this
I have created a program which takes a text file full of 3 letter
I'm probably doing this all wrong. I have a text file full of data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.