Stringstream 变量不以二进制代码显示?

发布于 2024-12-18 19:28:14 字数 1124 浏览 2 评论 0原文

为什么 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 技术交流群。

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

发布评论

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

评论(2

死开点丶别碍眼 2024-12-25 19:28:14

因为您使用了 printf 错误。最好根本不要使用printf

std::cout << query.str();

另外,在这里指定 binary 几乎没有用,并且 in/out 是多余的(stringstream 默认情况下是在/出去)。

Because you're using printf wrong. It's best not to use printf at all.

std::cout << query.str();

Also, specifying binary here is pretty much useless, and in/out are superfluous (stringstream is by default in/out).

橪书 2024-12-25 19:28:14

stringstream::binary 并没有做你想象的那样。

stringstream::binary 使流不会将系统换行符转换为 C++ 换行符。 stringstream::text 使流将系统换行符转换为 C++ 换行符。
这两者都不会使流作为“二进制代码”读取或写入。

C++ 换行符是 \n,而 Windows 系统换行符是 \r\n(两个字符!),而 Linux 仅使用 \n 。我听说 Mac 系统换行符是 \n\r 但我无法确认。

另外,正如 Oli CharlesworthCat Plus Plus 观察到,printf 不适用于 C++ 对象。请改用 std::cout,或将 std::string 转换为 printf 可以理解的 const char*

从您的评论来看,您想要类似

std::vector<char> query(buff.size()*2+1);
int len =  mysql_real_escape_string(mysql, &query[0], (const char*)&buff[0], query.size());
query.resize(len);

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. Use std::cout instead, or convert the std::string into a const char* that printf understands.

Judging by your comment, you want something along the lines of

std::vector<char> query(buff.size()*2+1);
int len =  mysql_real_escape_string(mysql, &query[0], (const char*)&buff[0], query.size());
query.resize(len);

This will escape binary data in buff for safe insertion into a mysql query.

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