在二进制文件中写入/读取 std::复杂值

Writing/reading std::complex values in binary file

本文关键字:std 复杂 读取 二进制文件      更新时间:2023-10-16

我假装将std::complex<float>std::vector存储在二进制文件中,然后再次将该数据加载到 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。

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

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

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

调用运算符>> of复杂> and not读取which 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());

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