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 6069195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:47:47+00:00 2026-05-23T09:47:47+00:00

I have a binary file that contains numbers of a type double. The example

  • 0

I have a binary file that contains numbers of a type double.
The example input file is available here: http://www.bobdanani.net/download/A.0.0
I would like to read the file and print the numbers in it.
This is what I have done:

char* buffer;
int length;
string filename = "A.0.0";
ifs.open (filename.c_str(), ios::in | ios::binary);
// get length of file:
ifs.seekg (0, ios::end);
length = ifs.tellg();
ifs.seekg (0, ios::beg);
// allocate memory:
buffer = new char [length];
// read data as a block:
ifs.read (buffer,length);
ifs.close();

cout.write (buffer,length);
cout << buffer << endl;

delete[] buffer;

I have also tried to use a type casting to double when printing the number, but I got strange characters. What is the best way to do this? I need the data of this binary file as an input to a function for a parallel program. But this is out of the scope of this question.

  • 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-05-23T09:47:47+00:00Added an answer on May 23, 2026 at 9:47 am

    While I could be wrong, since you said the number is separated by a tab/space, I’m willing to be this is actually ASCII data, and not raw binary data. Therefore the best way to work with the floating point value would be to use the operator>> on the ifstream object and then push that into a double. That will do an automatic conversion of the input value into a double, where-as what you’ve done will merely copy the character bytes that compose a floating point value, but are not a floating point value themselves. Additionaly, if you were trying to output your buffer like a string, you haven’t explicitly null-terminated it, so it’s going to keep reading up the stack until it encounters a null-terminator or you get a segmentation fault due to accessing memory the OS isn’t allowing you to access off the top of the stack. But either way, in the end, your buffer won’t be a representation of a double data-type.

    So you would have something like:

    double my_double_val;
    
    ifs.open (filename.c_str());
    
    if (ifs)
    {
        ifs >> my_double_val;
    }
    else
    {
        cerr << "Error opening file" << endl;
    }
    
    ifs.close();
    
    cout << "Double floating point value: " << my_double_val << endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have a binary file, that contains 32 bit numbers. Each 32bit number
What I have in my project is the binary file that contains list of
Let's say we have a binary file that contains 2 bytes that form an
I have a binary file in CSV format that contains multiple records. Each record
I have a binary file that I have to parse and I'm using Python.
I have binary data in a file that I can read into a byte
I have response stream from a ftp web request that returns binary file. I
I have a binary file which contains keys and after every key there is
Open a file in the Visual Studio binary editor that contains a null byte
I have a program written in C++, that opens a binary file(test.bin), reads it

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.