[英]vector<char> with zeros to string
I have a 我有一个
vector<char> sectionData;
and I would like to see its content.I try the following 我希望看到它的内容。我尝试以下内容
string sectionDataStr = string(sectionData.begin(),sectionData.end());
But I get only part of the sectionData presented in the string,since sectionData contains zeros.What can I do,if any case I want to read entire data to string? 但是我只获得字符串中提供的sectionData的一部分,因为sectionData包含零。我可以做什么,如果有的话我想读取整个数据到字符串?
I can`t use std::cout Thanks 我不能用std :: cout谢谢
Your problem doesn't exist. 你的问题不存在。 Here's a simple demonstration: 这是一个简单的演示:
#include <string>
#include <iostream>
int main()
{
std::cout << std::string { 'a', '\0', 'b', '\0', 'c' } << std::endl;
}
Now inspect the output: 现在检查输出:
$ ./a.out | hexdump -C
00000000 61 00 62 00 63 0a
## a \0 b \0 c \n
As you can see, it's all there. 正如你所看到的,它就在那里。
Alternatively (following your edit): 或者(编辑后):
#include <cstdio>
std::fwrite(sectionDataStr.data(), sectionDataStr.size(), stdout);
Your problem is not with the string, but with the output. 你的问题不是字符串,而是输出。 Depending on what you use, the output routine may stop at the first 0 in your string. 根据您使用的内容,输出例程可能会停止在字符串的前0位。 If you don't want that, then you might need to convert the buffer for output using something like: 如果您不想这样,那么您可能需要使用以下内容将缓冲区转换为输出:
std::replace( sectionData.begin(), sectionData.end(), '\0', ' ' )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.