如何创建 C++来自带有 null(0) 字符的 char 数组的 istringstream?
我有一个 char 数组,其中随机位置包含空字符。我尝试使用这个数组(encodedData_arr)创建一个iStringStream,如下所示,
我使用这个iStringStream将二进制数据(Iplimage的imagedata)插入到MySQL数据库blob字段(使用MySQL Connector/C++的setBlob(istream *is))它只存储直到第一个空字符的字符。
有没有办法使用带有空字符的 char 数组创建 iStringStream?
unsigned char *encodedData_arr = new unsigned char[data_vector_uchar->size()];
// Assign the data of vector<unsigned char> to the encodedData_arr
for (int i = 0; i < vec_size; ++i)
{
cout<< data_vector_uchar->at(i)<< " : "<< encodedData_arr[i]<<endl;
}
// Here the content of the encodedData_arr is same as the data_vector_uchar
// So char array is initializing fine.
istream *is = new istringstream((char*)encodedData_arr, istringstream::in || istringstream::binary);
prepStmt_insertImage->setBlob(1, is);
// Here only part of the data is stored in the database blob field (Upto the first null character)
I have a char array which contains null characters at random locations. I tried to create an iStringStream using this array (encodedData_arr) as below,
I use this iStringStream to insert binary data(imagedata of Iplimage) to a MySQL database blob field(using MySQL Connector/C++'s setBlob(istream *is) ) it only stores the characters upto the first null character.
Is there a way to create an iStringStream using a char array with null characters?
unsigned char *encodedData_arr = new unsigned char[data_vector_uchar->size()];
// Assign the data of vector<unsigned char> to the encodedData_arr
for (int i = 0; i < vec_size; ++i)
{
cout<< data_vector_uchar->at(i)<< " : "<< encodedData_arr[i]<<endl;
}
// Here the content of the encodedData_arr is same as the data_vector_uchar
// So char array is initializing fine.
istream *is = new istringstream((char*)encodedData_arr, istringstream::in || istringstream::binary);
prepStmt_insertImage->setBlob(1, is);
// Here only part of the data is stored in the database blob field (Upto the first null character)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
字符串中的空字符没有什么特别的
,当然如果你这样做
它会将其解释为转换为
std::string
的 C 风格字符串,因此将停止在\0< /code>,不将其视为真正的内容字节。明确告诉
std::string
大小允许它使用任何空字节作为真实内容数据。如果你想直接从char数组中读取,可以使用
strstream
,它会直接从
data
提供的数据中读取,最多N
字节。strstream
已被正式声明为“已弃用”,但它仍将出现在 C++0x 中,因此您可以使用它。或者,如果您确实需要像这样从原始char*
中读取数据,则可以创建自己的streambuf
。There is nothing special about null characters in strings
Of course if you do
It will interpret that as a C-style string converted to
std::string
, and thus will stop at the\0
, not taking it as a real content byte. Tellingstd::string
the size explicitly allows it to consume any null byte as real content data.If you want to read directly from a char array, you can use
strstream
That will directly read from the data provided by
data
, up toN
bytes.strstream
is declared "deprecated" officially, but it's still going to be in C++0x, so you can use it. Or you create your ownstreambuf
, if you really need to read from a rawchar*
like that.您如何检查这一点?如果你只是将它转储到 cout,它可能会停在第一个 nul 处。您需要逐个字符地打印。
How are you checking this? If you are just dumping it to cout, its probably stopping at the first nul. You will need to print character by character.