简体   繁体   English

如何从具有null(0)字符的char数组创建C ++ istringstream?

[英]How to create C++ istringstream from a char array with null(0) characters?

I have a char array which contains null characters at random locations. 我有一个char数组,在随机位置包含空字符。 I tried to create an iStringStream using this array (encodedData_arr) as below, 我尝试使用此数组(encodedData_arr)创建一个iStringStream,如下所示,

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. 我使用这个iStringStream将二进制数据(Iplimage的imagedata)插入MySQL数据库blob字段(使用MySQL Connector / C ++的setBlob(istream * is)),它只存储第一个空字符的字符。

Is there a way to create an iStringStream using a char array with null characters? 有没有办法使用带有空字符的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)

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. 它会将其解释为转换为std::string的C风格字符串,因此将停止在\\0 ,而不是将其作为实际内容字节。 Telling std::string the size explicitly allows it to consume any null byte as real content data. 告诉std::string显式允许它使用任何空字节作为真实内容数据。

If you want to read directly from a char array, you can use strstream 如果要直接从char数组中读取,可以使用strstream

std::istrstream iss(data, N);

That will directly read from the data provided by data , up to N bytes. 这将直接读取数据提供的data ,最多N个字节。 strstream is declared "deprecated" officially, but it's still going to be in C++0x, so you can use it. strstream正式声明为“已弃用”,但它仍将在C ++ 0x中,因此您可以使用它。 Or you create your own streambuf , if you really need to read from a raw char* like that. 或者你创建自己的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;
};

// 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)
  { }
};

// Here only part of the data is stored in the database blob field (Upto the first null character) //这里只有部分数据存储在数据库blob字段中(Up到第一个空字符)

How are you checking this? 你怎么检查这个? If you are just dumping it to cout, its probably stopping at the first nul. 如果你只是把它倾倒到cout,它可能会在第一个时间停止。 You will need to print character by character. 您需要逐个字符地打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM