user-0 提问: 遇到snprintf() 疑惑！

回复:
user: user-1
content: 提醒：snprintf是开源的。

user: user-1
content: 崩溃的时候在弹出的对话框按相应按钮进入调试，按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处，看不懂时双击下一行，直到能看懂为止。

user: user-2
content: 你在其他地方使用都正常，就某个位置，那就贴你的数据内容瞧瞧，估计是内容导致。 你也可尝试讲数组中间插入结束符号，然后你又取超过一个结束符号。 例如： aa[]={0x1,0x2,'\0',0x3,0x4} ,然后取len=5，看看0x03 估计是取不到的。

user: user-3
content: 多半是out_32char_PlainTxt分配的空间不够

user: user-0
content: 引用 3 楼 github.com/andarm 的回复:我尝试使用qt5.10 没有你说的len+0 才是需要输出。    char buf[20]={0};     snprintf(buf,4,"%s","12345"); // 需求是输出123.    len+1      qDebug()<<"show debug:"<<buf<<endl;  ------------------------------------------ 调试实际输出： 123   ； 所以你的问题应该和你的数据内容有关，是否数据中有结束符‘\0’  这个函数我在其他地方使用，都是正常的，实际输入字符长度+1；但唯独在这个函数中，出现异常，我想不明白是怎么回事。

user: user-2
content: 我尝试使用qt5.10 没有你说的len+0 才是需要输出。    char buf[20]={0};     snprintf(buf,4,"%s","12345"); // 需求是输出123.    len+1      qDebug()<<"show debug:"<<buf<<endl;  ------------------------------------------ 调试实际输出： 123   ； 所以你的问题应该和你的数据内容有关，是否数据中有结束符‘\0’

user: user-0
content: snprintf()会自动在末尾加'\0'，若长度不+1，则将原字符串最后一字符舍去了。为了保证原字符串不变，需长度+1； 而我这里，+1之后，最后一个字符并不是0，而是非可显示字符（ASCCC值为171的。）

user: user-1
content: _snprintf, _snwprintf Write formatted data to a string.  int _snprintf( char *buffer, size_t count, const char *format [, argument] ... );  int _snwprintf( wchar_t *buffer, size_t count, const wchar_t *format [, argument] ... );  Routine Required Header Compatibility  _snprintf <stdio.h> Win 95, Win NT  _snwprintf <stdio.h> or <wchar.h> Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  _snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned. _snwprintf returns the number of wide characters stored in buffer, not counting the terminating null wide character. If the storage required to store the data exceeds count wide characters, then count wide characters are stored in buffer and a negative value is returned.  Parameters  buffer  Storage location for output  count  Maximum number of characters to store  format  Format-control string  argument  Optional arguments  Remarks  The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) in buffer. Each argument (if any) is converted and output according to the corresponding format specification in format. The format consists of ordinary characters and has the same form and function as the format argument for printf. If copying occurs between strings that overlap, the behavior is undefined.  _snwprintf is a wide-character version of _snprintf; the pointer arguments to _snwprintf are wide-character strings. Detection of encoding errors in _snwprintf may differ from that in _snprintf. _snwprintf, like swprintf, writes output to a string rather than to a destination of type FILE.  Generic-Text Routine Mappings  TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined  _sntprintf _snprintf _snprintf _snwprintf    Example  /* SPRINTF.C: This program uses sprintf to format various  * data and place them in the string named buffer.  */  #include <stdio.h>  void main( void ) {    char  buffer[200], s[] = "computer", c = 'l';    int   i = 35, j;    float fp = 1.7320534f;     /* Format and print various data: */    j  = sprintf( buffer,     "\tString:    %s\n", s );    j += sprintf( buffer + j, "\tCharacter: %c\n", c );    j += sprintf( buffer + j, "\tInteger:   %d\n", i );    j += sprintf( buffer + j, "\tReal:      %f\n", fp );     printf( "Output:\n%s\ncharacter count = %d\n", buffer, j ); }   Output  Output:    String:    computer    Character: l    Integer:   35    Real:      1.732053  character count = 71   Stream I/O Routines  See Also   sprintf, fprintf, printf, scanf, sscanf, vprintf Functions