Question

I've got a file with some doubles (9 to be exact, for a 3x3 matrix) written in binary. I'm trying to make use of them as the real double value they represent (the number ej: 5.66 / -1.882 / etc..).

They are placed in the file one after another without any spaces: 3.245-2.453.253...... and they are exactly 9 numbers.

Then I have an struct with a pointer pointing to a vector of 9 slots (0-8) which I want to fill with the value of the doubles, SO, here's what I'm trying so far:

struct input{
    double data*;
    double myvector[9];
};

and then

input test;
test.data = test.myvector;
ifstream f(file,ios::binary);

f.read(reinterpret_cast<char *>(test.data),sizeof(double)*3*3);

Then when I went to check the values with some simple std::cout I got some weird numbers printed into console:

-0.1667 was printed as: -1,57218e+26

Is this supposed to happen? If not, what I'm doing wrong? In case I'm doing it right, what do I have to do with this value to get my "-0.1667"?

Thank you.

EDIT: By the time they created the file they had a pointer (pointing to a vector of doubles like my example) and they just made a f.write(whatever you put here); and that's what I mean when I sayd the numbers are one after another, there are no black characters in between any double there.

EDIT 2: full code:

#include <iostream>
#include <fstream>

using namespace std;

struct input{
    double data*;
    double myvector[9];
};

int main(int argc, char * argv[]){

    input test;
    test.data=test.myvector;

    ifstream f(argv[1], ios::binay); // argv[1] is the file EJ: ./program myfile
    f.read(reinterpret_cast <char *> (test.data), sizeof(double)*9); // being 9 values.

    for(int i=0;i<9;i++){

        cout << test.data[i] << endl;

    }
return 0;
}
Was it helpful?

Solution

Real numbers are stored as floating-point numbers. floating-point arithmetic is not exact: The value of the number is stored as a significant number of fixed digits, and an exponent of fixed digits.

When you try to print a floating-point value as a string, the number is formatted in "decimal-real-number" format. For this formatting, the value of the number is rounded (You want a representation with certain precission, not infinite digits).

Because of this formatting, what you see as "-0.1667" its really the rounded form of the real value. The other notation, -1.572218e+26 , is much similar to the true floating-point representation.

I supposed that when you talk about "-0.1667", you are talking about the original value you printed to the file. But -1.667 is the value that you see when outputs the value of the variable (on the console, for example), not? This is the point: -1.667 is a rounded value. The true value is "-1.572218e+26".

You are storing the flotating-point values in a file in binary-mode, so you print in the file the real representation of the number (The binary representation). When you read the file, you read the same value that you stored before. When you puts this value in std::cout, cout prints the value, without rounding (This can be configured).

If you want rounded format, you could use setprecision to configure the format.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top