vsnprintf 文档中 "encoding error" 的含义是什么?

What is the meaning of "encoding error" in the vsnprintf documentation?

C++ online docs for vsnprintf中表示

 If an encoding error occurs, a negative number is returned.

在这种情况下,"encoding error" 是什么意思,能否给出此类错误的示例?

它指的是格林奇指出的字符串编码错误。我们可以使用此代码重现负 return 值,因为在调用 wctomb:

时,129 在日语 (932) 代码页中是无效的宽字符
int call_vsnprintf(char* buf, int max, char* format, ...)
{
    va_list args;
    va_start(args, format);

#pragma warning (suppress : 4996)
    int result = vsnprintf(buf, max, format, args);

    va_end(args);

    return result;
}

int _tmain(int argc, _TCHAR* argv[])
{
    setlocale(LC_ALL, ".932");
    char dest[100];
    wchar_t wbuf[2];
    wbuf[0] = 129;
    wbuf[1] = 0;

    //this will be -1
    int result = call_vsnprintf(dest, sizeof(dest), "%ls", wbuf);
}

注意:这是在 Windows 上进行的,但如果它不可移植,可以通过在亚洲代码页中搜索强制 wctomb 为 return -1 的 widechars 轻松修复。

感谢 James Kuyper on Google Groups 几乎完整的回答。

让我确认 Debian Millie Smith 的回答:

char dest[100];
wchar_t wbuf[2];
wbuf[0] = 129;
wbuf[1] = 0;
int result = snprintf(dest, sizeof(dest), "%ls", wbuf);

结果为-1。