[英]What is the meaning of “encoding error” in the vsnprintf documentation?
正如 grinch 指出的那樣,它指的是字符串編碼錯誤。 我們可以使用此代碼重現負返回值,因為在調用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 返回 -1 的寬字符來輕松修復。
感謝Google Groups 上的 James Kuyper 提供了幾乎完整的答案。
讓我確認 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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.