c ++ - 保存“std :: vector< unsigned char>”的内容。到文件 [英] c++ - Save the contents of a "std::vector<unsigned char>" to a file
问题描述
我使用下面的函数writeFileBytes将 std :: vector< unsigned char>
的内容写入文件。我想使用类型unsigned char来保存文件(注意是为char转换)。这个问题的原因是因为unsigned char与任何字节(例如,00000000位)兼容。当我们使用char时,我们对某些无效字符的操作有一些问题。
查看此主题什么是最合适的向量类型保存文件的字节?有关00000000位(1字节)问题的更多信息。
void writeFileBytes(const char * filename,std :: vector< unsigned char>& fileBytes){
std :: ofstream file(filename,std :: ios :: out | std :: ios :: binary );
file.write(fileBytes.size()?(char *)& fileBytes [0]:0,
std :: streamsize(fileBytes.size()));
}
writeFileBytes(xz.bin,fileBytesOutput);
是否有一种方法可以使用unsigned char
$ b
UPDATE I:
是否有一种方式可以使用unsigned char - > 是!
按照krzaq的指南!
void writeFileBytes(const char * filename,std :: vector< unsigned char>& fileBytes){
std :: ofstream file(filename,std: :ios :: out | std :: ios :: binary);
std :: copy(fileBytes.cbegin(),fileBytes.cend(),
std :: ostream_iterator< unsigned char>(file)
}
UPDATE II p>
正如我在下面评论的:
...unsigned char似乎有更高的级别当我们尝试将这8位('00000000'位)转换为'char'时,我们没有'unsigned char'的值,'unsigned char'我们有一个无效的/无法识别的'char'value,but we have ...
查看此主题什么是最合适的类型的向量来保存文件的字节? 更多信息!
没关系, char *
可以用来访问任何类型的数据,它会正常工作。但是如果您不想使用显式匹配,则可以使用 std :: copy
和 std :: ostreambuf_iterator
:
copy(fileBytes.cbegin(),fileBytes .cend(),
ostreambuf_iterator< char>(file));
或者,您可以致电
copy(fileBytes.cbegin(),fileBytes.cend(),
ostream_iterator< char>(file)
但它会做同样的事情,只能更慢。
Btw:你不能传递一个空指针到 write
,那就是UB。
I use the function below "writeFileBytes" to write the contents of a std::vector<unsigned char>
to a file. I want to use the type "unsigned char" to save the file (note that is made a cast for "char"). The reason for that question is because "unsigned char" has compatibility with any byte ("00000000" bits, for example). When we use "char" we have some problems with the manipulation of certain "invalid" chars.
See this topic What is the most suitable type of vector to keep the bytes of a file? for more information about the issues with "00000000" bits (1 byte).
void writeFileBytes(const char* filename, std::vector<unsigned char>& fileBytes){
std::ofstream file(filename, std::ios::out|std::ios::binary);
file.write(fileBytes.size() ? (char*)&fileBytes[0] : 0,
std::streamsize(fileBytes.size()));
}
writeFileBytes("xz.bin", fileBytesOutput);
Is there a way to use the type "unsigned char" natively to write to a file?
This concern really make sense?
UPDATE I:
Is there a way to use the type "unsigned char" natively to write to a file? -> YES!
Following the krzaq's guidelines!
void writeFileBytes(const char* filename, std::vector<unsigned char>& fileBytes){
std::ofstream file(filename, std::ios::out|std::ios::binary);
std::copy(fileBytes.cbegin(), fileBytes.cend(),
std::ostream_iterator<unsigned char>(file));
}
UPDATE II:
This concern really make sense? -> In some ways, YES!
As I comment below...
"...'unsigned char' seems to have a 'higher level of compatibility' (include '00000000' bits). When we try to convert these 8 bits ('00000000' bits) to 'char' we have no value unlike 'unsigned char'. With 'unsigned char' we have an invalid/unrecognized 'char' value, but we have..."
See this topic What is the most suitable type of vector to keep the bytes of a file? for more information!
It doesn't matter, char*
can be used to access any kind of data and it'll work correctly. But if you don't want to use the explicit cast, maybe use std::copy
and std::ostreambuf_iterator
:
copy(fileBytes.cbegin(), fileBytes.cend(),
ostreambuf_iterator<char>(file));
alternatively, you can call
copy(fileBytes.cbegin(), fileBytes.cend(),
ostream_iterator<char>(file));
But it will do the same thing, only possibly slower.
Btw: you can't pass a null pointer to write
, that's UB.
这篇关于c ++ - 保存“std :: vector< unsigned char>”的内容。到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!