1

I was looking for a substitute of gotoxy for Dev C++ v5.11 (gcc compiler) and found this:

void gotoxy(int x,int y) {
    printf("%c[%d;%df", 0x1b, y, x);
}

After this when I tried to call this function as follows:

int main() {
    gotoxy(20, 10);
    printf("Hello");
    return 0;
}

Output was not as expected:

<-[10;20fHello

This was printed in top left most corner of screen (i.e. 1,1) instead of (20,10).

Please give me suggestions that what I can do to use gotoxy in my code.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Jaladh Singhal
  • 393
  • 4
  • 10
  • 3
    That looks like an ANSI escape sequence. I don't think the windows console supports those. – melpomene Jul 09 '17 at 10:05
  • You can use [this open-source CONIO implementation for MinGW/Dev-C++](http://conio.sourceforge.net/). After you install the Devpak, you'll need to configure your compiler paths to point to the `include` and `lib` directories in your Dev-C++ install directory (e.g. `C:\Program Files (x86)\Dev-C++ 5.11\include` for headers and `C:\Program Files (x86)\Dev-C++ 5.11\lib` for libs). Then you need to ensure you link the lib. 64-bit builds require `-lconio_64` or `-lconio_unicode_64`, depending on whether you `#define UNICODE`. 32-bit just drops the `_64`. Then just `#include ` as needed. –  Jul 09 '17 at 13:44
  • After doing a little digging through updates to [the docs for SetConsoleMode](https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx) using the Wayback Machine, the Windows 10 Anniversary Update added some VT100 support for console output, and it seems the Creators Update in Spring 2017 added more support, including input. See [Console Virtual Terminal Sequences](https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx) for more info about how to use them and examples of how to enable the features. Not sure it'll work on Win8.1 or older though :( –  Jul 09 '17 at 16:52
  • Note that those features don't appear to work with `wineconsole` yet, for those who are wondering. –  Jul 09 '17 at 17:09
  • per this 'https://en.wikipedia.org/wiki/ANSI_escape_code' windows before 10 did not incorporate the terminal manipulation capabilities. However, there are drivers that you could use like `ansi.com` to enable the ansi escape sequences. – user3629249 Jul 09 '17 at 22:28

2 Answers2

4

Your ANSI escape sequence is incorrect, it should be \033[%d;%dH, but it seems your terminal does not support ANSI VT100 escape sequences at all. In Windows, there might be a configuration setting to enable it, VT100 emulation is standard in most modern operating systems terminals (unix, linux, BSD, OS/X...).

Here is the modified code:

#include <stdio.h>

void gotoxy(int x, int y) {
    printf("\033[%d;%dH", y, x);
}

int main(void) {
    gotoxy(20, 10);
    printf("Hello\n");
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • @JaladhSinghal: you can accept one of the answers by clicking on the grey checkmark below its score. – chqrlie Jul 14 '17 at 23:09
0

One can also use this.

#include <windows.h>

void gotoxy(int x,int y){
COORD V={x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), V);
}
Prakhar Tomar
  • 23
  • 1
  • 5
  • This solution works *only* on Windows and *only* if your application allocates a native Windows console. On the other hand, the correct ANSI sequence will work on most other operating systems and terminal emulators. – sapanoia Jun 19 '21 at 18:54
  • Yes. I should have mentioned that. But, since the post was about Dev C++ I thought it was already understood, as Dev C++ is available for Windows platform only and it can create Windows or console-based C++ programs. Anyways, thank you for mentioning that. – Prakhar Tomar Jun 20 '21 at 14:52