I pretend to store a std::vector
of std::complex<float>
in a binary file, and to later load that data into a std::vector again. I have checked similar questions in SO and came up with the following code:
#include <complex>
#include <iostream>
#include <memory>
#include <fstream>
#include <iterator>
#include <random>
#include <vector>
using namespace std;
typedef vector<complex<float>> complex_vector;
float get_random(){
static std::default_random_engine e;
static std::uniform_real_distribution<> dis(-10, 10);
return dis(e);
}
int main(){
// Random number generator
srand(static_cast<unsigned> (time(0)));
// Create a vector with 25 random complex<float> values
std::shared_ptr<vector<complex<float>>> buffer = std::make_shared<vector<complex<float>>>();
for(unsigned int i=0; i<25; i++){
buffer->push_back(complex<float>(get_random(), get_random()));
}
// Write those values into a binary file
std::string binFileName = "aux.bin";
std::ofstream rawFile(binFileName, std::ios::binary);
for(unsigned int i=0; i<buffer->size(); i++){
rawFile.write(reinterpret_cast<const char*>(&buffer->at(i)), sizeof(complex<float>));
}
rawFile.close();
// Load the binary file into the buffer
std::ifstream input(binFileName, std::ios::binary);
complex_vector auxBuffer{std::istream_iterator<complex<float>>(input), std::istream_iterator<complex<float>>()};
unsigned int samplesRead = auxBuffer.size();
std::cout << samplesRead;
return 0;
}
Output:
0
What am I missing?
EDIT: After NathanOliver's answer, this is how my code looks like now:
#include <complex>
#include <iostream>
#include <memory>
#include <fstream>
#include <iterator>
#include <random>
#include <vector>
using namespace std;
typedef vector<complex<float>> complex_vector;
float get_random(){
static std::default_random_engine e;
static std::uniform_real_distribution<> dis(-10, 10);
return dis(e);
}
int main(){
// Random number generator
srand(static_cast<unsigned> (time(0)));
// Create a vector with 25 random complex<float> values
std::shared_ptr<complex_vector> buffer = std::make_shared<complex_vector>();
for(unsigned int i=0; i<25; i++){
buffer->push_back(complex<float>(get_random(), get_random()));
}
// Write those values into a binary file
std::string binFileName = "aux.bin";
std::ofstream rawFile(binFileName, std::ios::binary);
rawFile.write(reinterpret_cast<const char*>(buffer->size()), sizeof(buffer->size()));
rawFile.write(reinterpret_cast<const char*>(buffer->data()), sizeof(std::complex<float>) * buffer->size());
rawFile.close();
// Load the binary file into the buffer
std::ifstream input(binFileName, std::ios::binary);
complex_vector::size_type nSamples;
input.read(reinterpret_cast<char*>(nSamples), sizeof(complex_vector::size_type));
std::cout << nSamples;
complex_vector *destination = new complex_vector(25);
input.read(reinterpret_cast<char*>(destination->data()), sizeof(std::complex<float>) * nSamples);
return 0;
}
I'm getting a SIGSEGV in the first call to write function.
Also, I don't understand why I write , but I have to read .