0

Right now I have a C++ program that reads in two columns of data from a text file using something like

while(!file.eof())
{
    double a, b;
    file >> a >> b; // extracts 2 floating point values separated by whitespace

    // do something with them
}

Now I want to adjust this code to read in two columns of binary data from a .bin file. I still want to treat the values as doubles in the rest of my program. What is the easiest way to accomplish this?

EDIT:

I am writing the binary data like this in a python program. I think of it as being in two columns.

import struct
c = struct.Struct('=ff')
with open('numbers.bin', 'w+') as outf:
    for r, k in nonzero:
        outf.write(c.pack(r, k))
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
user1764386
  • 4,181
  • 6
  • 24
  • 39

1 Answers1

1

Saying you want to read in two 'columns' of binary data doesn't make sense.

Columns are an artifact of visualizing data for the purposes of readability by the human eye. When you're reading/writing binary data, don't think about how data would look to a human. Just imagine a large segment of 1s and 0s on a hard disk that are stored contiguously. Forget about rows... forget about lines... etc.

You need to use fstream 'read' and 'write' commands. See this link for documentation on fstream: http://www.cplusplus.com/reference/fstream/fstream/

2to1mux
  • 735
  • 5
  • 7