如何从一个具有null(0)个字符的字符数组创建C ++ istringstream? [英] How to create C++ istringstream from a char array with null(0) characters?
问题描述
我有一个char数组,在随机位置包含空字符。我尝试使用这个数组(encodedData_arr)如下创建一个iStringStream:
我使用这个iStringStream插入二进制数据(Iplimage的映像)到MySQL数据库blob字段使用MySQL连接器/ C ++的setBlob(istream * is))它只存储字符直到第一个空字符。
有没有办法使用具有空字符的字符数组创建iStringStream?
unsigned char * encodedData_arr = new unsigned char [data_vector_uchar-> size()];
//分配向量的数据< unsigned char>到encodingData_arr
for(int i = 0; i {
cout< data_vector_uchar-> at(i)<< :<< encodingData_arr [i]<< endl;
}
//这里encodeData_arr的内容与data_vector_uchar相同
//所以char数组正在初始化。
istream * is = new istringstream((char *)encodedData_arr,istringstream :: in || istringstream :: binary);
prepStmt_insertImage-> setBlob(1,is);
//这里只有部分数据存储在数据库blob字段(直到第一个空字符)
std: :istringstream iss(std :: string(data,N));
setBlob(& iss);
当然,如果你
std :: istringstream iss(haha a null:\0);
它将把它解释为C风格的字符串转换为 std :: string
,因此将在 \0
处停止,而不将其视为实际内容字节。告诉 std :: string
,显式地允许它使用任何空字节作为实际内容数据。
如果要直接从字符数组中读取,可以使用 strstream
std :: istrstream iss(data,N);
这将直接从数据
,最多 N
个字节。 strstream
官方声明为deprecated,但它仍然是在C ++ 0x,所以你可以使用它。或者你创建自己的 streambuf
,如果你真的需要从一个原始的 char *
读取。
struct myrawr:std :: streambuf {
myrawr(char const * s,size_t n){
setg const_cast< char *>(s),
const_cast< char *>(s),
const_cast< char *>(s + n)
}
};
struct hasb {
hasb(char const * s,size_t n)
:m(s,n)
{}
myrawr m;
};
//使用base-from-member idiom
struct myrawrs:private hasb,std :: istream {
myrawrs(char const * s,size_t n)
:hasb(s,n),
std :: istream(& static_cast< hasb *>(this) - > m)
{}
}
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)
There is nothing special about null characters in strings
std::istringstream iss(std::string(data, N));
setBlob(&iss);
Of course if you do
std::istringstream iss("haha a null: \0");
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. Telling std::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
std::istrstream iss(data, N);
That will directly read from the data provided by data
, up to N
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 own streambuf
, if you really need to read from a raw char*
like that.
struct myrawr : std::streambuf {
myrawr(char const *s, size_t n) {
setg(const_cast<char*>(s),
const_cast<char*>(s),
const_cast<char*>(s + n));
}
};
struct hasb {
hasb(char const *s, size_t n)
:m(s, n)
{ }
myrawr m;
};
// using base-from-member idiom
struct myrawrs : private hasb, std::istream {
myrawrs(char const *s, size_t n)
:hasb(s, n),
std::istream(&static_cast<hasb*>(this)->m)
{ }
};
这篇关于如何从一个具有null(0)个字符的字符数组创建C ++ istringstream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!