问题描述
有谁知道为什么会出现在C标准库中没有 snwprintf
功能?
Does anyone know why there is no snwprintf
function in the C standard library?
我知道的swprintf
的,但是,这并不具有真, wchar_t的
版本相同的语义的的snprintf
。据我所知,有使用实现一个 snwprintf
函数没有简单的方法 [V]的swprintf
:
I am aware of swprintf
, but that doesn't have the same semantics of a true, wchar_t
version of snprintf
. As far as I can tell, there is no easy way to implement an snwprintf
function using [v]swprintf
:
与的snprintf
,的swprintf
不返回所需的缓冲区的大小;如果提供的缓冲区是不够的,它只是返回-1。这是从失败由于编码错误没有区别,所以我不能继续重试与逐渐变大的缓冲区希望它最终会取得成功。
Unlike snprintf
, swprintf
does not return the necessary buffer size; if the supplied buffer is insufficient, it simply returns -1. This is indistinguishable from failure due to encoding errors, so I can't keep retrying with progressively larger buffers hoping that it eventually will succeed.
我想我可以设置缓冲区的最后一个元素是不可NUL,叫的swprintf
,并承担发生截断如果该元素是NUL之后。然而,是保证工作?本标准不指定缓冲区应该是什么状态,如果的swprintf
失败。 (与此相反,的snprintf
描述了写入字符和被丢弃。)
I suppose I could set the last element of the buffer to be non-NUL, call swprintf
, and assume that truncation occurred if that element is NUL afterward. However, is that guaranteed to work? The standard does not specify what state the buffer should be in if swprintf
fails. (In contrast, snprintf
describes which characters are written and which are discarded.)
推荐答案
见拉里·琼斯给出的答案<一个href=\"http://groups.google.com/group/comp.std.c/browse_frm/thread/5c1686a4338eaaa4/0e7c7ce3950aa2fb?lnk=gst&q=snwprintf#0e7c7ce3950aa2fb\"相对=nofollow>这里。
See the answer given by Larry Jones here.
从本质上讲,在C95加入的swprintf
,而的snprintf
在C99和因为许多实现加入已经返回的数字所需的字符(的snprintf
),它似乎做一个有用的东西,这是被规范的行为。他们没有想到的是把它加(其中没有这种行为几年前加入)打破与的swprintf
向后兼容的行为是足够重要。
Essentially, swprintf
was added in C95 while snprintf
was added in C99 and since many implementations already returned the number of required characters (for snprintf
) and it seemed a useful thing to do, that was the behavior that was standardized. They didn't think that behavior was important enough to break backwards compatibility with swprintf
by adding it (which was added without that behavior several years earlier).
这篇关于为什么C没有一个snwprintf功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!