snprintf 和 Visual Studio 2010 [英] snprintf and Visual Studio 2010
问题描述
我很遗憾在一个项目中使用 VS 2010 卡住了,并注意到以下代码仍然无法使用非标准兼容编译器构建:
#include #include int main (void){字符缓冲区[512];snprintf(buffer, sizeof(buffer), "SomeString");返回0;}
(编译失败,错误:C3861: 'snprintf': identifier not found)
我记得在 VS 2005 中就是这种情况,并且很震惊地看到它仍然没有得到修复.
有谁知道微软是否有计划将其标准 C 库移至 2010 年?
Short story: Microsoft 终于在 Visual Studio 2015 中实现了 snprintf.在早期版本中,您可以如下模拟.
<小时>长版:
这是 snprintf 的预期行为:
int snprintf( char* buffer, std::size_t buf_size, const char* format, ... );
<块引用>
最多将 buf_size - 1
个字符写入缓冲区.所结果的字符串将以空字符结尾,除非buf_size
为零.如果 buf_size
为零,则不写入任何内容并且buffer
可能是一个空指针.返回值是数量假设无限 buf_size
会写入的字符,不计算终止空字符.
Visual Studio 2015 之前的版本没有一致的实现.取而代之的是非标准扩展,例如 _snprintf()
(不会在溢出时写入空终止符)和 _snprintf_s()
(可以强制执行空终止,但在溢出时返回 -1,而不是本应写入的字符数).
VS 2005 及更高版本的建议回退:
#if defined(_MSC_VER) &&_MSC_VER <1900#define snprintf c99_snprintf#define vsnprintf c99_vsnprintf__inline int c99_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap){整数计数 = -1;如果(大小!= 0)count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);如果(计数== -1)计数 = _vscprintf(格式,AP);返回计数;}__inline int c99_snprintf(char *outBuf, size_t size, const char *format, ...){整数计数;va_list ap;va_start(AP,格式);count = c99_vsnprintf(outBuf, size, format, ap);va_end(ap);返回计数;}#万一
I'm unfortunate enough to be stuck using VS 2010 for a project, and noticed the following code still doesn't build using the non-standards compliant compiler:
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
char buffer[512];
snprintf(buffer, sizeof(buffer), "SomeString");
return 0;
}
(fails compilation with the error: C3861: 'snprintf': identifier not found)
I remember this being the case way back with VS 2005 and am shocked to see it still hasn't been fixed.
Does any one know if Microsoft has any plans to move their standard C libraries into the year 2010?
Short story: Microsoft has finally implemented snprintf in Visual Studio 2015. On earlier versions you can simulate it as below.
Long version:
Here is the expected behavior for snprintf:
int snprintf( char* buffer, std::size_t buf_size, const char* format, ... );
Writes at most
buf_size - 1
characters to a buffer. The resulting character string will be terminated with a null character, unlessbuf_size
is zero. Ifbuf_size
is zero, nothing is written andbuffer
may be a null pointer. The return value is the number of characters that would have been written assuming unlimitedbuf_size
, not counting the terminating null character.
Releases prior to Visual Studio 2015 didn't have a conformant implementation. There are instead non-standard extensions such as _snprintf()
(which doesn't write null-terminator on overflow) and _snprintf_s()
(which can enforce null-termination, but returns -1 on overflow instead of the number of characters that would have been written).
Suggested fallback for VS 2005 and up:
#if defined(_MSC_VER) && _MSC_VER < 1900
#define snprintf c99_snprintf
#define vsnprintf c99_vsnprintf
__inline int c99_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap)
{
int count = -1;
if (size != 0)
count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
if (count == -1)
count = _vscprintf(format, ap);
return count;
}
__inline int c99_snprintf(char *outBuf, size_t size, const char *format, ...)
{
int count;
va_list ap;
va_start(ap, format);
count = c99_vsnprintf(outBuf, size, format, ap);
va_end(ap);
return count;
}
#endif
这篇关于snprintf 和 Visual Studio 2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!