Stringstream 变量不以二进制代码显示?
为什么 stringstream 查询变量不起作用?
std::stringstream query (stringstream::in | stringstream::out | stringstream::binary);
for(vector<uchar>::iterator it=buff.begin();it !=buff.end();it++)
{
query<<*it;
}
cout<<query.str().length();
printf("output:%s",query);
[编辑]
Mat data=image;//image is ROI (50X50) from IplImage* getting Matrix data.
std::vector<uchar> buff;
std::vector<int> p;
p.push_back(CV_IMWRITE_PNG_COMPRESSION);
p.push_back(9);
cv::imencode(".png", data, buff);//for ROI image data to "png" vector buff.
std::vector<char> query(buff.size()*2+1); //MooingDuck's codes...
int len = mysql_real_escape_string(handle, &query[0], (const char*)&buff[0], query.size());
query.resize(len);
我收到错误:
0040CF5F jmp _escape_string_for_mysql+0F2h (40CFA2h) 0040CF61 mov al,byte ptr [edi] <----------------------------------------错误点 0040CF63 movsx ecx,al 0040CF66 cmp ecx,5Ch 0040CF69 ja _escape_string_for_mysql+0E5h (40CF95h)
why stringstream query variable does not work?
std::stringstream query (stringstream::in | stringstream::out | stringstream::binary);
for(vector<uchar>::iterator it=buff.begin();it !=buff.end();it++)
{
query<<*it;
}
cout<<query.str().length();
printf("output:%s",query);
[EDIT]
Mat data=image;//image is ROI (50X50) from IplImage* getting Matrix data.
std::vector<uchar> buff;
std::vector<int> p;
p.push_back(CV_IMWRITE_PNG_COMPRESSION);
p.push_back(9);
cv::imencode(".png", data, buff);//for ROI image data to "png" vector buff.
std::vector<char> query(buff.size()*2+1); //MooingDuck's codes...
int len = mysql_real_escape_string(handle, &query[0], (const char*)&buff[0], query.size());
query.resize(len);
I get the error:
0040CF5F jmp _escape_string_for_mysql+0F2h (40CFA2h) 0040CF61 mov
al,byte ptr [edi] <---------------------------------------error point
0040CF63 movsx ecx,al 0040CF66 cmp ecx,5Ch 0040CF69 ja
_escape_string_for_mysql+0E5h (40CF95h)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您使用了
printf
错误。最好根本不要使用printf
。另外,在这里指定
binary
几乎没有用,并且in
/out
是多余的(stringstream
默认情况下是在/出去)。Because you're using
printf
wrong. It's best not to useprintf
at all.Also, specifying
binary
here is pretty much useless, andin
/out
are superfluous (stringstream
is by default in/out).stringstream::binary
并没有做你想象的那样。stringstream::binary
使流不会将系统换行符转换为 C++ 换行符。stringstream::text
使流将系统换行符转换为 C++ 换行符。这两者都不会使流作为“二进制代码”读取或写入。
C++ 换行符是
\n
,而 Windows 系统换行符是\r\n
(两个字符!),而 Linux 仅使用\n
。我听说 Mac 系统换行符是\n\r
但我无法确认。另外,正如 Oli Charlesworth 和 Cat Plus Plus 观察到,
printf
不适用于 C++ 对象。请改用std::cout
,或将std::string
转换为printf
可以理解的const char*
。从您的评论来看,您想要类似
This will escape binary data in
buff
的内容,以便安全插入到 mysql 查询中。stringstream::binary
does not do what you think it does.stringstream::binary
makes the stream not translate system newlines as a C++ newlines.stringstream::text
makes streams transalte system newlines into C++ newlines.Neither of which makes streams read or write as "binary code".
A C++ newline is
\n
, whereas a Windows system newline is\r\n
(two characters!), and linux uses just\n
. I've heard that Mac system newlines are/were\n\r
but I can't confirm that.Also, as Oli Charlesworth and Cat Plus Plus observed,
printf
does not work with C++ objects. Usestd::cout
instead, or convert thestd::string
into aconst char*
thatprintf
understands.Judging by your comment, you want something along the lines of
This will escape binary data in
buff
for safe insertion into a mysql query.