如何将 printf 存储到变量中?
我想使用类似于 C 中 printf 的方法来存储格式化字符串。
char *tmp = (char *)sqlite3_column_text(selectstmt, 2);
const char *sqlAnswers = printf("select key from answer WHERE key = %s LIMIT 5;", tmp);
后者显然是一个错误。
I want to store a formatted string using something similar to what printf does in C.
char *tmp = (char *)sqlite3_column_text(selectstmt, 2);
const char *sqlAnswers = printf("select key from answer WHERE key = %s LIMIT 5;", tmp);
The latter is an error obviously.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 sprintf 来完成此操作,但不能单独(安全)。在正常的系统上,使用
snprintf
两次,一次是为了找出要使用的大小,第二次是为了实际执行它。这取决于snprintf
在空间不足时返回所需的字符数。 Linux、BSD 和 C99 兼容系统可以做到这一点; Windows 通常不会。在后一种情况下,您需要分配一个初始缓冲区,并在snprintf
失败时分配一个更大的缓冲区(循环直到snprintf
成功)。但在 C99 上,以下内容将起作用:但是,为了构建 SQL,最好使用 准备好的声明。它们避免了 SQL 注入漏洞(并且经常需要
sprintf
)。使用它们,您可以准备语句“select key from answer where key = ? limit 5;”,然后使用参数tmp
执行它。 SQL 引擎放入字符串并消除首先确保其正确转义的需要。You can do it with
sprintf
, but not alone (safely). On a sane system, usesnprintf
twice, once to find out the size to use and the second time to actually do it. This depends onsnprintf
returning the number of characters needed when it runs out of room. Linux, BSD, and C99-compatible systems do this; Windows typically does not. In the latter case, you'll need to allocate an initial buffer and allocate a bigger one ifsnprintf
fails (in a loop untilsnprintf
succeeds). But on C99, the following will work:However, for building SQL, it's far better to use prepared statements. They avoid SQL injection vulnerabilities (and frequently the need for
sprintf
). With them, you would prepare the statement "select key from answer where key = ? limit 5;", and then execute it with the parametertmp
. The SQL engine puts in the string and removes the need to make sure it's properly escaped first.您需要
sprintf()
。You want
sprintf()
.如果您使用的是 gnu 或 BSD libc,您也许可以使用
asprintf
,它会自动分配正确大小的缓冲区。If you're using gnu or BSD libc you may be able to use
asprintf
, which allocates a buffer of the correct size automatically.我实际上使用 sqlite3_bind_text 输入通配符,而不是通过 sprintf 生成:
I am actually using sqlite3_bind_text to input my wildcard instead of generating through sprintf:
Michael Ekstrand 代码很好,但您需要多次复制和粘贴它。我在一个函数中使用此代码
是否存在缓冲区溢出问题?到目前为止我还没有遇到任何问题。
编辑。
好吧,我有一个问题,因为我正在使用 Arduino。它使用内存并且不会丢弃它,因此使用后需要删除它。
The Michael Ekstrand code is good, but you will need to copy and paste it various times. I use this code in one function
Does it has problem with buffer overflow? Until now I don't have problem with it.
Edit.
Ok, I have a problem because I am working with Arduino. It use memory and don't drop it, so you need to delete it after the use.
在 Windows 上,您可以使用 sprintf_s 来添加缓冲区溢出保护,就像 Michael E 所说的那样。
http://msdn.microsoft.com/en-us /library/ce3zzk1k(VS.80).aspx
On windows you can use sprintf_s which adds buffer overflow protection like Michael E was saying.
http://msdn.microsoft.com/en-us/library/ce3zzk1k(VS.80).aspx