0

I want to move the cursor to the x,y position so i use:

#include <stdio.h>

int main()
{

    int x = 10, y = 10   
    printf("\e[%d;%df",y,x);
    printf("HelloWorld");

}

but it's output is:

[10;10fHelloWorld

I try to change from \e to %c, 0x1B as an example file from my friend but it still not work. It only work in my friend file. What should I do to make it work? or Should I use windows.h instead?

Ary Silna
  • 17
  • 1
  • 7
  • To go to X, Y position i think `printf("\033[%d;%dH", (x), (y))` should work. – Gaurav Pathak Nov 07 '17 at 12:15
  • Possible duplicate of [How to position the input text cursor in C?](https://stackoverflow.com/questions/26423537/how-to-position-the-input-text-cursor-in-c) – Gaurav Pathak Nov 07 '17 at 12:18
  • It still not work. it's output still be the same(change from f->H) – Ary Silna Nov 07 '17 at 12:18
  • For me, it's working! What OS are you using? I am using Cygwin on windows. – Gaurav Pathak Nov 07 '17 at 12:20
  • I use Windows 10 – Ary Silna Nov 07 '17 at 12:21
  • 3
    It **should** work on most terminals (given you encode the escape correctly, e.g. `\x1b` or `\033`). But just don't do it, escape-sequences aren't perfectly portable. Use `curses` instead. –  Nov 07 '17 at 12:21
  • 2
    Windows cmd.exe does not implement this kind of terminal emulation – Ctx Nov 07 '17 at 12:22
  • @Ctx it **does** in windows 10, but the support is turned off by default when a new process launches for backwards compatibility. –  Nov 07 '17 at 12:24
  • @FelixPalmen Hm, I have other information, can you provide a reference? – Ctx Nov 07 '17 at 12:25
  • 2
    @Ctx here's [the official one](https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences). Note that `cmd.exe` still disables `ENABLE_VIRTUAL_TERMINAL_PROCESSING` before launching a console application. BTW, `cmd.exe` is only the shell, it indeed doesn't implement these sequences, the console window does. –  Nov 07 '17 at 12:29
  • @FelixPalmen ic, interesting, thank you. – Ctx Nov 07 '17 at 12:30

1 Answers1

2

The \e in your code is wrong, but when you repace it by the ASCII code of an escape character and also change f to H to make it the correct sequence for cursor positioning, your code will work on all terminals that implement ANSI escape sequences. This includes many terminals on Linux and other *nix-like systems.

The windows console also supports ANSI escape sequences starting from Windows 10, but this support is disabled by cmd.exe by default for backwards compatibility, so to make this code work on Windows 10, you have to explicitly enable this mode:

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

// this line is only for older versions of windows headers (pre Win 10):
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004

int main(void)
{
    // enable ANSI sequences for windows 10:
    HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD consoleMode;
    GetConsoleMode(console, &consoleMode);
    consoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
    SetConsoleMode(console, consoleMode);

    int x = 10, y = 10;
    printf("\x1b[%d;%dH", y, x);
    printf("HelloWorld");
}

But that all said, you should really consider using curses instead. There are two widespread implementations of the curses library, ncurses and pdcurses. Both work on a wide variety of systems. My personal recommendation would be ncurses for *nix systems and pdcurses for windows. If you just #include <curses.h> in your code, you can link to both libraries as you want. What you get is full control of your terminal/console output without relying on possibly non-portable escape sequences (it will work on earlier versions than Windows 10 as well).

For learning how to use curses, consider the NCURSES Programming HOWTO.