C++ 使用basic_of Stream写入字节数据序列
我有一个类型为C++ 使用basic_of Stream写入字节数据序列,c++,c++17,ofstream,C++,C++17,Ofstream,我有一个类型为std::list的info\u list变量,我希望迭代该变量,并将数据以二进制格式顺序保存到文件中。以下是我尝试过的: void write_data() { std::basic_ofstream<std::byte> file("data.bin", std::ios::binary); for (decltype(auto) info : info_list) { file.write(info.second
std::list
的info\u list
变量,我希望迭代该变量,并将数据以二进制格式顺序保存到文件中。以下是我尝试过的:
void write_data() {
std::basic_ofstream<std::byte> file("data.bin", std::ios::binary);
for (decltype(auto) info : info_list) {
file.write(info.second, info.first);
}
}
void write_data(){
流文件的std::basic_(“data.bin”,std::ios::binary);
对于(decltype(自动)信息:信息列表){
file.write(info.second,info.first);
}
}
std::pair
的first
是second
指针指向的数据大小
我对这些C++流不太好。文件将被保存为空。我做错了什么
我希望避免使用C特性来实现这一点
编辑:
我尝试使用
std::ofstream
而不是std::basic_of stream
,效果很好。。。为什么std::basic_of stream
在这里不起作用?我遗漏了什么吗?以下是您遗漏的内容:当使用std::basic_of stream
时,您实际上是在实例化std::basic_of stream
问题在于,因为标准专门化(行为定义良好的类型)是:
std::char\u特征
特征
特征
特征
特征
与使用标准库中定义的专门化(即
std::char\u traits
)的std::ofstream
不同,您的代码使用std::char\u traits
。要继续这样的实施,您必须。我想知道如何做到这一点。。。但我的总体印象是,超出标准定义流(char、wchar__t)并不总是像您所想/希望的那样微不足道。是一本有趣的书(尽管它已经3年了)。继续使用decltype(auto)
和auto&
都被扣除到相同的类型中。@JoãoPaulo没有提到在一个范围内使用decltype(auto)
loop@RemyLebeau同样来自官方cpp:“(…)但是,decltype(auto)除此之外,并不打算成为广泛使用的功能。特别是,尽管它可以用于声明局部变量,但这样做可能只是一种反模式,因为局部变量的引用不应依赖于初始化表达式”。所以,是的,“合法”并不意味着它好用。