簡體   English   中英

在二進制文件中寫入/讀取 std::complex 值

[英]Writing/reading std::complex values in binary file

我假裝將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;
}

我在第一次調用 write 函數時得到一個 SIGSEGV。

另外,我不明白我為什么要寫,但我必須閱讀。

您不能將二進制寫入與格式化讀取混合使用,而這正是您現在正在做的。 即使文件以二進制模式打開

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

調用運算符 >> of complex> and not read which is what you should use if you write the data with write` 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它們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());

我個人喜歡第一個選項,因為它看起來更干凈,但如果這是性能敏感的,那么二進制模式通常更快。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM