-4

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)

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
Tansistem
  • 1
  • 1
  • 1
    What's the output? Note that if your chars are non-ASCII codes, the output will be quite garbled. Best to test this with only ascii chars in the buff vector. Also, you can't printf a stringstream as a %s. query.str().c_str() can be printf'ed though. – jzila Nov 29 '11 at 23:20
  • You may want to add a question. We have to guess what you _want_/_expect_ as well as what you _are getting_. Also, what's up with the printf? – sehe Nov 29 '11 at 23:21
  • Are you sure it hasn't worked? What are the type and contents of `buff` (the type *should* be `vector` to match the iterator, but...)? If you just want the contents of `buf` in a string, why not just use a string directly instead of using a stringstream? – Jerry Coffin Nov 29 '11 at 23:23
  • **bold** Thank you very much, you are right, main issue is buff vector has png (graphic data). I will pass to mysql blob data type. How could it send? – Tansistem Nov 29 '11 at 23:37
  • buf is not string "png" or "jpg" type binary code. – Tansistem Nov 29 '11 at 23:39
  • thank you for your answer jerry. – Tansistem Nov 30 '11 at 00:07

2 Answers2

2

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).

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
1

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.

Community
  • 1
  • 1
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • You are great, the main issue is buff vector has "png" type binary data. I want to pass mysql blob type column record. I know mysql_real_escape but, I search day by day did not find solution!!! – Tansistem Nov 29 '11 at 23:31
  • @Tansistem: did I address the real question then? – Mooing Duck Nov 29 '11 at 23:44
  • Great Mooing Duck. Build code OK!.But giving runtime error!in my opinion buff vector is uchar. Mat data=image; std::vector buff; std::vector p; p.push_back(CV_IMWRITE_PNG_COMPRESSION); p.push_back(9); cv::imencode(".png", data, buff); – Tansistem Nov 30 '11 at 00:01
  • Please update the question with the full information. The code you posted does not contain `mysql_real_escape_string`. – Mooing Duck Nov 30 '11 at 00:07
  • Hi Mooing Duck. It is getting runtime error. Sory for english lang. Mat data=image;//image is ROI (50X50) from IplImage* getting Matrix data. std::vector buff; std::vector 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 query(buff.size()*2+1); //your codes... int len = mysql_real_escape_string(handle, &query[0], (const char*)&buff[0], query.size()); query.resize(len); – Tansistem Nov 30 '11 at 00:21
  • @Tansistem: I put the code in the question for you. What is the full text of the runtime error, and on what line does it occur? – Mooing Duck Nov 30 '11 at 00:30
  • Hi,Mooing,i think mysql function pointer problem??? – Tansistem Nov 30 '11 at 00:43
  • 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) – Tansistem Nov 30 '11 at 00:51
  • somethink similar:http://stackoverflow.com/questions/4474076/c-opencv-access-violation-reading-location-0x02176000 – Tansistem Nov 30 '11 at 01:19