I noticed my code didn't output enough padding spaces, one short in my case. I have such code as follows,
wchar_t *wchar_text = L"Juli - Ein Gruß";
printf(" 12345678901234567890\n");
printf("|%-20ls|\n", wchar_text);
It outputs,
12345678901234567890 |Juli - Ein Gruß |
There should be five spaces not just four. I guess printf()
still count the bytes of the string even I gives %ls
and "ß"
is two bytes in UTF-8 encoding. So the length of bytes is 16, therefore only 4 space are padded.
Since I need to output in UTF-8, not UTF-32, I can't just simply replace printf
with wprintf
.
My solution is
wchar_t buf[21] = L"";
swprintf(buf, 21, L"%-20ls", wchar_text);
printf("|%ls|\n", buf);
I use swprintf
with a text buffer to do padding in wide-char, then printf
with that text buffer instead of the orignal text.