Reputation: 75
I am having problems with a terminal emulator I am working on, and I have managed to narrow down the problem to a weird cursor behavior in the Windows console. In a regular cmd, when you write character exactly to the end of the line, the cursor jumps to the next line. (See below picture)
When I try to achieve the same with printf in a program I write, the cursor stays on the same character. (See below picture)
I have managed to achieve the "cmd-like" result by printing the hacky " \b"
, but I am trying to find a better way to do that (i.e., the first character at the next line might be meaningful, and I don't want to erase it. Reading that character would of course not be elegant).
I am looking for a way to make that happen automatically. Some configuration of the console maybe? Does anybody here have any idea how to do that?
A code example (assuming the console has a default size of 80):
int main() {
printf("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
fflush(stdout);
getc();
return 0;
}
If you run this code, after the print, the cursor will stay on the character 0 at the end of the line. My question is how to make the cursor appear on the beginning of the next line, without changing the hard coded string.
Upvotes: 3
Views: 1714
Reputation: 75
I found the solution: https://learn.microsoft.com/en-us/windows/console/setconsolemode
The flag DISABLE_NEWLINE_AUTO_RETURN seems to be enabled by default in VS.
Upvotes: 2
Reputation: 1415
In windows the end of line is represented as "\r\n" not (as in unix) "\n". The \r is a carriage return and the \n is a newline. So emitting "\r\n" for a newline should solve your problem.
https://en.wikipedia.org/wiki/Newline
Upvotes: 0