在二进制文件中写入/读取 std::复杂值
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;
}
我在第一次调用写入函数时得到了一个 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());
我个人喜欢第一个选项,因为它看起来更干净,但如果这是性能敏感的,那么二进制模式通常更快。
相关文章:
- 如何检查两个 std::向量在小于 O(n) 的时间复杂度内是否相等
- C++中 std::map 的运行时复杂度是多少?
- C++ - 最坏情况和平均情况插入时间复杂度在 std::unordered_map <int,int>?
- C++ 中 std::map 的空间复杂度是多少?
- "+=" 操作在类型之间不起作用 std::复杂<double>和__complex__双精度
- std::regex 是否保证了最坏情况下的时间复杂度?
- std::结构复杂,使编译速度变慢
- QVECTOR与STD :: VECTER的复杂类型
- 简单的方法将隐式类型促销添加到std ::复杂类操作员
- 在二进制文件中写入/读取 std::复杂值
- STD ::复杂的初始化问题
- std::map的时间复杂度是多少
- Visual Studio std::复杂性能
- 在不使用 std::copy 函数的情况下编写这个复杂的复制结构体
- 一个清晰的函数的时间复杂度是多少,根据大 O,是 std::map
- 在这种情况下比较 std::vector 或 std::set 以获得时间复杂度 - 更有效率
- 'std::p artition()' 的时间复杂度
- std::vector::erase的时间复杂度
- std::deque::erase函数的时间复杂度是多少
- 是否可以使用std::atomic将类的复杂成员函数变为原子?