Why does the following code
const std::string text = "str";
HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coords = { 0, 0 };
DWORD written = 0;
WriteConsoleOutputCharacterA(stdout_handle, text.c_str(), text.size(), coords, &written);
WORD attributes = FOREGROUND_GREEN;
WriteConsoleOutputAttribute(stdout_handle, &attributes, text.size(), coords, &written);
results in this:

What am I doing wrong? How can I fix it?
&attributes points to an array of length one, a single green attribute. But you claim the array is text.size() long. As a result, you copy random stack content to the next 2 cells. That happens to look red-on-red.
Solution:
std::vector<WORD> attributes(text.size(), FOREGROUND_GREEN);
WriteConsoleOutputAttribute(stdout_handle, &attributes[0] ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With