std::ifstream::get() return double?

Hi,

I am trying to read a binary file in C++.

For that I am using the get() method from std::ifstream.

I can read int values stored in a binary file, this is OK.

But I can not read double values, that is the problem. Because get() return a int value.

Is there some way to std::ifstream::get() return a double?

Someone sugests another method to read double values stored in binary files?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdlib.h>
#include <stdio.h>
#include <ios>
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

class readBin {
public:
    ifstream fileBin;
    ifstream::pos_type filesize;
    vector<double> byte;
    void instructions(void);
    void readArgs(string,string,string);
    void readUint8(string);
    void noInputFile(void);
    readBin();
    readBin(const readBin& orig);
    virtual ~readBin();
private:

};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "readbin.h"

void readBin::readArgs(string arg_0, string arg_1, string filename) {
    //    cout << "treating args" << endl;
    if (arg_0 == "-t") {
        if (arg_1 == "uint8") {
            readUint8(filename);
        }
    }
}

void readBin::readUint8(string filename) {
    cout << "Processing binary uint8 file: " << filename << endl;

    fileBin.open(filename.c_str(), ios::binary);

    fileBin.seekg(0, ios::end);
    filesize = fileBin.tellg();
    cout << "filesize: " << filesize << endl;
    fileBin.seekg(0, ios::beg);

    for (double i = 0; i < filesize; i++) {
        cerr << i << " ";
        cerr << fileBin.get() << endl;
//        byte.push_back(fileBin.get());
//        cout << "byte[" << i << "]: " << (long) byte.at(i) << endl;
    }

    fileBin.close();
}

void readBin::instructions(void) {
    cout << "We expect you run this program in this way:" << endl;
    cout << "readbin -t type binaryfile.dat" << endl;
    cout << "type can be: uint8, double" << endl;
}

void readBin::noInputFile(void) {
    cout << "No input File." << endl;
}

readBin::readBin() {
}

readBin::readBin(const readBin& orig) {
}

readBin::~readBin() {
}
Well, for starters, are you storing doubles? And if so, why?
Text files are much better to store floating point numbers.
Yes, I am storing double values in a binary file. (I am using Matlab for that.)

In fact, I am not sure if it is correct to say "I am storing doubles". May be it is more correct to say I am storing int values from 1 to 1000000 in a binary file.

The problem is that the code above can read from 1 to 255 only. For the next values it returns 255 to me.

I think is because get() return an int...

Do you suggest another way to do this?

Thanks.
You'll have to piece them together. You'll need to know the file's endianness (you can read the article on endianness on Wikipedia).
As an example, to make a 16-bit integer out of big endian bytes, you just do (byte[0]<<8)|byte[1]. Read the article and you'll understand how to do it for larger integers.
Hey helios, can you explain in more details please...I did not understant what you want to say with "...piece them together."

I am not sure that it was needed to spend attention with endianess.I have not considerated this yet and, in fact, I can read the data from the binary file. The point is that get() is only able to return a value between 0 and 255.

But I will read the article on endianness on Wikipedia.

Thanks.
Check out the write_word() and read_word() functions I provide here, with example use:
http://www.cplusplus.com/forum/beginner/11431/page1.html#msg53963

Before you use it, though, you should definitely find out exactly how the data you are reading is stored. As already stated by helios, storing floating point values in a file in binary format is not a good idea. It is better just to store the textual representation (like "12.934e-7"), then read/write it using the iostream extraction/insertion operators (operator>> and operator<<).

Hope this helps.
A file is a collection of bytes. An integer is a smaller collection of bytes. When you're using C++, the language handles manipulating these bytes for you, but you don't have that luxury with files, so you have to do it yourself.

Oh. Look up the meaning of the shift operators (<< left and >> right) and bitwise OR (|) if you don't know them already.
I am not storing floating point values. When I had used double in matlab(to produce the binary file) it was because uint8 was not creating values higher than 255.
So they are floating point values. The fact that you were using them to store integers is irrelevant. Also, why not use uint32 or uint64 if you just needed larger integers?
Why write them to a binary file in the first place?

You'll save yourself all this grief, and more grief you don't know about yet, by just using a text file.
Yeah...saving a couple of bytes isn't worth it...it also means you can go in an manually edit the file if you feel like.
helios,

I do not have this knowledge you is telling about. But is very interesting, all the things, I will try it.

Duoas and firedraco,

I tell to my boss about the benefits of the text file but he is the boss.

Thanks for all.
Topic archived. No new replies allowed.