0

I wrote code to print the same text in 2 locations and it works until I insert the cursor movement line and while the code still runs, the result is garbled, the move command gets printed.

#include <stdio.h>

int main(void) {
    char str[] = "text"; // Declare string
    printf("%s", str);   // Print string
    printf("\033[4B");   // Move down 4 lines;
    printf("%s", str);   // Print string
    char ch = _getch();
}

VS Debug window prints it corrently:

text



    text

CMD prints

text←[4Btext

I tried installing .NET Framework and using a different compiler (gcc to be specific) and even tried running this on 4 different machines, they all produced the same result.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
Hydrough
  • 3
  • 1
  • 1
    It’s not a function of the compiler. It requires terminal support for the escape sequence. Cmd doesn’t support it. – Mark Tolonen Nov 02 '22 at 16:12
  • This isn't about the escape sequence though, the "Move 4 lines down" code is the broken part – Hydrough Nov 02 '22 at 16:14
  • @Hydrough as already mentioned, cmd doesn't support escape sequences, therefore it is quite normal that your code doesn't work in cmd. Installing . NET Framework won't help, it's totally unrelated. Switching to another compiler won't of course help either. – Jabberwocky Nov 02 '22 at 16:16
  • @Hydrough "\033[4B" is the escape sequence for "move 4 lines down". So if your terminal doesn't support escape sequences, your code appears to be broken. The problem cannot be repaired within your code. – Jabberwocky Nov 02 '22 at 16:18
  • Alright, well that's a bummer. Is there a way to relocate the cursor with code in CMD or will I have to get a different program to run the code? What's the solution? – Hydrough Nov 02 '22 at 16:19
  • @Hydrough you need a terminal that supports ANSI escape codes, period. Or you need to write your program using the Windows graphic interface, but that's another, very different story. – Jabberwocky Nov 02 '22 at 16:29
  • Maybe this helps: https://stackoverflow.com/questions/16755142/how-to-make-win32-console-recognize-ansi-vt100-escape-sequences – Jabberwocky Nov 02 '22 at 16:30
  • 1
    @MarkTolonen that's not true. cmd is a **shell**, like bash, ksh, powershell... Shell don't do anything with the ANSI sequence. Only conhost.exe, Windows terminal, xterm... are terminals. cmd will attach to some terminal when running, and [modern conhost.exe supports ANSI sequence](https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences) although it's a bad terminal and one should use Windows terminal anyway – phuclv Nov 02 '22 at 16:38
  • 1
    @phuclv Yes terminology. The cmd *shell* doesn't enable terminal support for escape sequences *by default*. – Mark Tolonen Nov 02 '22 at 16:55
  • @MarkTolonen no, the **console/terminal** which is conhost.exe doesn't enable ANSI sequence by default. It has nothing to do with cmd. Windows terminal supports it and should be used instead – phuclv Nov 05 '22 at 03:53

1 Answers1

2

The console doesn't enable virtual terminal sequences by default. It requires extra code:

#include <windows.h>
#include <stdio.h>
#include <conio.h>

int main(void) {
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD mode;
    GetConsoleMode(h, &mode);
    mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
    SetConsoleMode(h, mode);

    char str[] = "text"; // Declare string
    printf("%s", str);   // Print string
    printf("\033[4B");   // Move down 4 lines;
    printf("%s", str);   // Print string
    _getch();
}

Output (cmd):

text



    text
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • Oh thank you this work so well thank you so much! A project I spent 2+ weeks on is not lost after all, all thanks to you! – Hydrough Nov 02 '22 at 19:46
  • `GetConsoleMode` already implies that it has to do with the console, not the shell (cmd or powershell) – phuclv Nov 05 '22 at 03:53