I'm trying to read a file that contains various paths. I'm unsure of the languages used in these paths. After reading about the Windows API, I thought using wchar_t*
might be a good idea to ensure all characters are handled correctly.
Here's what I'm doing:
- Read a file using
CreateFileW
.
- Read the content into a
char buffer[1024]
.
- Parse the buffer until I find new lines, then copy the content into a
char lineBuffer[1024]
.
- Convert it from
char*
to wchar_t*
using MultiByteToWideChar
.
- Print the
wchar_t*
buffer using wprintf(L"%ls\n");
.
I've looked at various code examples online and thought this should work. However, the output in the terminal only shows ??
.
I've tried this on Windows Terminal using both cmd
and pwsh
, and the issue persists. I've also tried changing the codepage with chcp
from 850 to 65001 without success.
Here's a minimal example that reproduces my issue:
#include <windows.h>
#include <stdio.h>
int main() {
const char* utf8String = "Hello, 世界!";
int utf8Length = strlen(utf8String); // this is 14
// Calculate the size needed for the wide character buffer
int wideCharSize = MultiByteToWideChar(CP_UTF8, 0, utf8String, utf8Length, NULL, 0);
// Allocate the wide character buffer
wchar_t* wideBuffer = (wchar_t*)malloc((wideCharSize + 1) * sizeof(wchar_t));
if (wideBuffer == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
// Perform the conversion
MultiByteToWideChar(CP_UTF8, 0, utf8String, utf8Length, wideBuffer, wideCharSize); // return value is not 0
// Null-terminate the wide character string
wideBuffer[wideCharSize] = L'\0';
// Use the wide character string (for demonstration, print it)
wprintf(L"%ls\n", wideBuffer);
// Free the allocated memory
free(wideBuffer);
return 0;
}
What am I missing? Is such a conversion even necessary?