利用snprintf和assert向字符串流输出信息
来源:互联网 发布:网络优化外泄最低范围 编辑:程序博客网 时间:2024/05/16 19:05
man snprintf
int snprintf(char *str, size_t size, const char *format, ...);
RETURN VALUE Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings). The functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte ('\0')). If the output was truncated due to this limit, then the return value is the number of charac‐ ters (excluding the terminating null byte) which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated. (See also below under NOTES.) If an output error is encountered, a negative value is returned.
将可变个参数(...)按照format格式化成字符串,然后将其复制到str中
(1) 如果格式化后的字符串长度 < size,则将此字符串全部复制到str中,并给其后添加一个字符串结束符('\0');
(2) 如果格式化后的字符串长度 >= size,则只将其中的(size-1)个字符复制到str中,并给其后添加一个字符串结束符('\0'),返回值为欲写入的字符串长度。
可以利用这点结合自定义的调试assert进行输出
#define Assert(cond, ...) \do { \if(!(cond)) { \fflush(stdout); \fprintf(stderr, "\33[1;31m"); \fprintf(stderr, __VA_ARGS__); \fprintf(stderr, "\33[0m\n"); \assert(cond); \} \} while(0)
#define print_asm(...) Assert(snprintf(assembly, 80, __VA_ARGS__) < 80, "buffer overflow!")
0 0
- 利用snprintf和assert向字符串流输出信息
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流 istringstream 和 ostringstream 的用法
- c++ 控制台流和字符串流
- 字符串流 istringstream 和 ostringstream 的用法
- 字符串流 istringstream 和 ostringstream 的用法
- 如何更好和更有效地向用户输出信息
- 如何使用标准字符串流istrstream,和itof
- IO流之字节数组流和字符串流
- [C++]字符串流 istringstream 和 ostringstream 的用法
- MPTCP - 3/4 OK -3- mptcp-tcpdump1.txt
- MATLAB size函数
- android MIME
- MPTCP - 3/4 OK -4- mptcp-tcpdump2.txt
- Acticle 7:javascript运动:怎么修复拖拽运动中的bug
- 利用snprintf和assert向字符串流输出信息
- Web移动端Fixed布局的解决方案
- Solr4.6用csv文件导入数据
- 欢迎使用CSDN-markdown编辑器
- MapReuce笔记六之输入类InputFormat
- POJ 1003 Hangover
- 收藏ES6相关网站
- OpenSSL加解密程序
- [模板]矩阵快速幂(以hdu1757为例