用引号写入缓冲区的一部分的最佳方法是什么?
对于 exaplme,我有一些缓冲区: const char* buf
以及下一个内容(mysql 数据包):
72 00 00 00 select * from `db` where (`name` = "Bill's car")
并且我需要仅用引号写入 ostream 查询。所以,结果应该是下一个:
select * from `db` where (`name` = \"Bill\'s car\")
我知道, << quote <<
将进行引用,而 ostream.write(buf,len)
将编写我需要的部分。
但对于两者来说最好的解决方案是什么?
For exaplme, I have some buffer : const char* buf
with next content (mysql packet):
72 00 00 00 select * from `db` where (`name` = "Bill's car")
and i need to write to ostream only query with quoting. So, result should be next:
select * from `db` where (`name` = \"Bill\'s car\")
I know, that << quote <<
will make quoting and ostream.write(buf,len)
will write part I need.
But what the best solution for both?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类似这样的事情应该做:
这会将缓冲区的内容逐个字符复制到输出流(在本例中为
std::cout
)。那么您无需担心处理报价。您唯一需要确保正确的是两个索引(sql 块的开始和结束)。
注意:这将打印出缓冲区中的内容,但不会转义引号。如果您需要转义引号,那么您需要采取不同的方法。例如,使用
for_each
和自定义函子来检查字符是否为'
或"
并根据需要转义...Something like this ought to do:
This copies the contents of the buffer character by character to the output stream (in this case
std::cout
). You don't need to worry about handling quotes then.The only things you need make sure are correct are the two indexes (start and end of chunk of sql).
NOTE: this will print out what is in the buffer, but will not escape the quotes. If you need to escape the quotes, then you'll need to take a different approach. e.g. use
for_each
and a custom functor to check if character is'
or"
and escaping as necessary...