Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用basic_of Stream写入字节数据序列_C++_C++17_Ofstream - Fatal编程技术网

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年了)。继续使用 STD::OsFrase:写到被改进(或固定,取决于您的观点)。在另一个注释中,在<代码>中< <代码>循环> <代码> DeCype(Auto)应该是代码> Auto&:<代码>(Auto&Inf:iNoFiLIST)< /Comp> @ ReMyLeBeAu为什么?如果在这种情况下
decltype(auto)
auto&
都被扣除到相同的类型中。@JoãoPaulo没有提到在一个范围内使用
decltype(auto)
loop@RemyLebeau同样来自官方cpp:“(…)但是,decltype(auto)除此之外,并不打算成为广泛使用的功能。特别是,尽管它可以用于声明局部变量,但这样做可能只是一种反模式,因为局部变量的引用不应依赖于初始化表达式”。所以,是的,“合法”并不意味着它好用。