在二进制文件

时间:2017-02-13 11:47:53

标签: c++ binary

我假装将std::vector std::complex<float>存储在二进制文件中,然后再将该数据加载到std :: vector中。我在SO中检查了类似的问题,并提出了以下代码:

#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;
}

输出:

0

我错过了什么?

编辑: 在NathanOliver的回答之后,这就是我的代码现在的样子:

#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;
}

我在第一次写函数调用时得到了一个SIGSEGV。

另外,我不明白为什么写作,但我必须阅读。

1 个答案:

答案 0 :(得分:1)

您无法将二进制写入与格式化读取混合,这正是您现在正在做的事情。即使文件以二进制模式打开

complex_vector auxBuffer{std::istream_iterator<complex<float>>(input), std::istream_iterator<complex<float>>()};

调用运算符&gt;&gt; of复杂&gt; and notwhich is what you should use if you write the data with写`。

您有两种方法可以解决这个问题。您可以执行格式化写入而不是像

这样的二进制
for (const auto& e : *buffer)
    rawFile << e << "\n"; // or space or any other white space delimiter

然后你会完全按照你的意思阅读它。

你的另一个选择是write向量的大小和向量的内容,然后read那些都回来了

// write the size of the vector
rawFile.write(reinterpret_cast<const char*>(&buffer->size()), sizeof(buffer->size()));
// write the whole contents of the vector in one go
rawFile.write(reinterpret_cast<const char*>(buffer->data()), sizeof(std::complex<float>) * buffer->size());

// and then to read it back in we get the size
complex_vector::size_type size;
input.read(reinterpret_cast<char*>(&size), sizeof(size));
// construct a vector of that size
complex_vector auxBuffer(size);
// and then read the data back into the vector
input.read(reinterpret_cast<char*>(auxBuffer.data()), sizeof(std::complex<float>) * auxBuffer->size());

我个人喜欢第一个选项,因为它看起来更干净但如果这对性能敏感,那么二进制模式通常会更快。

相关问题