的snprintf和Visual Studio 2010 [英] snprintf and Visual Studio 2010
问题描述
我不幸使用VS 2010的一个项目被卡住,并注意到以下code仍然没有建立使用非符合标准的编译器:
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT主要(无效)
{
字符缓冲区[512]; 的snprintf(缓冲液,的sizeof(缓冲液),SomeString); 返回0;
}
(失败,错误编译:C3861:'的snprintf':标识符找不到)
我记得这个既然如此方式回到2005年VS和感到震惊地看到它仍然没有得到解决。
是否有任何人知道,如果微软有没有计划把他们的标准C库到2010年?
短篇小说:在Visual Studio 2015年微软终于实现的snprintf在早期版本中,你可以按照以下模拟它
龙版本:
下面是snprintf的预期行为:
INT的snprintf(字符*缓冲区的std ::为size_t buf_size,为const char *格式,...);
最多
写入buf_size - 1
字符缓冲区。所结果的
文字串将与空字符被终止,除非
buf_size
是零。如果buf_size
为零,没有什么是书面和
缓存
可能是一个空指针。返回值是多少
这将被写入假设无限buf_size
字符,
不包括终止空字符。
块引用>唱片集的Visual Studio 2015年之前,不具有一致性的实现。还有如
_snprintf()
(不写溢出空终止符)和_snprintf_s()$,而不是非标准扩展C $ C>(可强制执行空终止,但将返回-1溢出,而不是将已写入的字符数)。
2005年VS高达回退建议:
#如果定义(_MSC_VER)及和放大器; _MSC_VER< 1900#定义的snprintf c99_snprintf
的#define vsnprintf c99_vsnprintf__inline INT c99_vsnprintf(字符* OUTBUF,为size_t的大小,为const char *格式,va_list的AP)
{
诠释计数= -1; 如果(大小!= 0)
数= _vsnprintf_s(OUTBUF,大小,_TRUNCATE,格式,AP);
如果(计数== -1)
数= _vscprintf(格式,AP); 返回计数;
}__inline诠释c99_snprintf(字符* OUTBUF,为size_t的大小,为const char *格式,...)
{
诠释计数;
va_list的AP; 的va_start(AP,格式);
数= c99_vsnprintf(OUTBUF,大小,格式,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屋!