I was learning how to use wchar, but I am unable to copy the char using snprintf(). I want to copy the text from a to b but it never prints the value. What am I doing wrong? And I want to use snprintf and nothing else.

#include <wchar.h>
#include <stdio.h>

void main() {

    printf("Start\n");
    wchar_t a[12];
    wchar_t b[] = L"984567";

    snprintf(a, sizeof(a), "%ls\0", b);

    printf("%ls\n", a);
    printf("%ls\n", b);

    printf("end\n");
}

//Output

Start

984567

end

有帮助吗?

解决方案 2

For an array T arr[10], sizeof arr returns the number of bytes occupied by the array (ie. sizeof(T) * 10 in this case.) However, swprintf expects the number of wchar_t in the destination buffer as its second argument.

The swprintf() function shall place output followed by the null wide character in consecutive wide characters starting at *ws; no more than n wide characters shall be written, including a terminating null wide character, which is always added (unless n is zero).

The below test code shows how the canary values are destroyed when you simply use sizeof a and the source wchar_t string is longer than the destination wchar_t buffer.

#include <wchar.h>
#include <stdio.h>

#define ARRLEN(a) (sizeof(a) / sizeof(a[0]))

int main() {
    struct S {
        int i;
        wchar_t a[4];
        int j, k, l;
    };
    struct S x = {}, y = {};
    wchar_t b[] = L"984567";
    int rcx, rcy;

    printf("Start\n");

    printf("%ld %ld\n", sizeof(x.a), sizeof(x.a[0]));

    rcx = swprintf(x.a, ARRLEN(x.a), L"%ls", b);
    rcy = swprintf(y.a, sizeof(y.a), L"%ls", b);

    printf("%#x %ls %#x %#x %#x %d\n", x.i, x.a, x.j, x.k, x.l, rcx);
    printf("%#x %ls %#x %#x %#x %d\n", y.i, y.a, y.j, y.k, y.l, rcy);
    printf("%ls\n", b);

    printf("end\n");
    return 0;
}

其他提示

snprintf is for writing to char buffers. To write to a wchar_t buffer, you need swprintf instead.

I know this is an old post, but I've done this method that returns a newline on windows. I stored the unicodes for "Carriage Return" and "Line Feed" inside a map, called _translator[] and then I created this function. This is a good example of how you can use swprintf.

wchar_t* NewLine() {
    wchar_t returning[2];
    swprintf(returning, sizeof(returning), L"%lc%lc", _translator[CARRIAGE_RETURN], _translator[LINE_FEED]);
    return returning;
}

Use swprintf.

#include <wchar.h>
#include <stdio.h>

int main()
{
    wchar_t a[ 10 ], b[] = L"984567";

    swprintf( a, sizeof( a ), L"%ls", b );

    printf("%ls\n", a);
}

Output: 984567

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top