如何创建 C++来自带有 null(0) 字符的 char 数组的 istringstream?

发布于 2024-09-01 08:59:45 字数 935 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心碎的声音 2024-09-08 08:59:45

字符串中的空字符没有什么特别的

std::istringstream iss(std::string(data, N));
setBlob(&iss);

,当然如果你这样做

std::istringstream iss("haha a null: \0");

它会将其解释为转换为 std::string 的 C 风格字符串,因此将停止在 \0< /code>,不将其视为真正的内容字节。明确告诉 std::string 大小允许它使用任何空字节作为真实内容数据。

如果你想直接从char数组中读取,可以使用strstream

std::istrstream iss(data, N);

它会直接从data提供的数据中读取,最多N字节。 strstream 已被正式声明为“已弃用”,但它仍将出现在 C++0x 中,因此您可以使用它。或者,如果您确实需要像这样从原始 char* 中读取数据,则可以创建自己的 streambuf

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

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)
  { }
};
月隐月明月朦胧 2024-09-08 08:59:45

//这里仅将部分数据存储在数据库blob字段中(直到第一个空字符)

您如何检查这一点?如果你只是将它转储到 cout,它可能会停在第一个 nul 处。您需要逐个字符地打印。

// Here only part of the data is stored in the database blob field (Upto the first null character)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文