簡體   English   中英

vsnprintf文檔中的“編碼錯誤”是什么意思?

[英]What is the meaning of “encoding error” in the vsnprintf documentation?

vsnprintfC++ 在線文檔中,有說明

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

在這種情況下,“編碼錯誤”是什么意思,能否舉個例子說明這種錯誤?

正如 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM