I am trying to write a string directly to the console buffer in C++ using WriteConsoleOutputCharacter. I read the contents of a file which contains multiple \n characters but when I write to the console buffer it doesn't write the rest of the file on the next line instead it writes ??. In notepad it says my file encoding is utf-8. I wondered if this had anything to do with it but when I print the file contents using cout the line breaks where ever it encounters \n.
std::ifstream file("debug.txt");
static HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD ir = 0;
if (file.is_open()) {
file.seekg(0, file.end);
int len = file.tellg();
std::cout << len;
file.seekg(0, file.beg);
char* string = (char*)calloc(len+1, sizeof(char));
string[len] = '\0';
file.read(string, len);
file.close();
COORD coord = { 0, 0 };
WriteConsoleOutputCharacterA(
console,
string,
len,
coord,
&ir
);
free(string);
This is how I read from the file and write to the console buffer. Any thoughts?