1

I have a problem with reading a binary file.

My code so far:

ifstream file("ns.bin", ios::in | ios::binary | ios::ate);
file.seekg(0, std::ios::end); 
size = file.tellg();  
file.seekg(0, std::ios::beg); 
buffer = new char[size];
file.read(buffer, size);
file.close();

double* double_values = (double*)buffer;//reinterpret as doubles

My question is: How do I get the doubles from double_values into a vector of this type:

vector<double> buffer2;

Or, if not possible: How do I get the number of doubles from double_values?

Hope that sb can help me thanks !

Jonas
  • 6,915
  • 8
  • 35
  • 53
Ildon
  • 47
  • 9

2 Answers2

2

Just copy it into the vector:

vector<double> buffer2(double_values, double_values + (size / sizeof(double)));

Explanation:

This uses the iterator constructor of std::vector (overload 5), which expects a begin iterator and an end iterator. Pointers are iterators, and to get the end iterator, you need to know how many elements are in the array.

Turns out, you know how many bytes the array is, so taking the total size and dividing it by the size of one double element will give you the total length of the array.

It then becomes trivial to find the end iterator by adding the size the begin iterator.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Russell Greene
  • 2,141
  • 17
  • 29
  • Thanks ! for you aswer and the good explanation :) i have another question maybe you have an idea :) i tried to Print the vector with cout and as limit i set the number of doubles size / sizeof(double)... and it works .. it prints every double (size / sizeof(double) it is also the right number ) but at the end it print also sth but you can see nothing it looks like he is printing spaces... so i tried it also with a 2d vector and here he is printin some 0 s ... do you have an idea why ? – Ildon Jan 20 '17 at 17:01
  • It's a bit hard to tell what exactly your problem is, but I would guess that you are trying to print one past the last element; `cout << buffer2[buffer2.size()]` is undefined behavior – Russell Greene Jan 20 '17 at 21:53
  • That won’t interpret binary data as doubles, it will convert each *individual* byte to a double in the range -128–127 (for the first N/8 bytes, and ignoring the rest). – Konrad Rudolph May 27 '22 at 12:51
  • @KonradRudolph I don't think that's true, considering `double_values` is a `double*`. I believe that would only be the case if it were a `signed char*` – Russell Greene Jun 01 '22 at 15:02
  • @RussellGreene Of course, you’re absolutely right. My apologies — I misread the question’s code. – Konrad Rudolph Jun 02 '22 at 12:53
0

First to the problem. It is completely answered here: How to initialize std::vector from C-style array?

const int size = 64;
char* chars = new char[64];
double* double_values = (double*)chars;

const int size_of_doubles = (sizeof(char)*size)/sizeof(double);
std::cout<<"Doubles: "<<size_of_doubles<<'\n';
std::vector<double> buffer2(double_values, double_values+size_of_doubles );
std::cout<<buffer2.size();
delete chars;

I'd also remark that it is unwise to directly copy variables into binary and back. Issues such as type size, endianity or (in case of text) encoding may arise and break your program on different computers.

Community
  • 1
  • 1
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778