Search code examples
cgccc++builderc99borland-c++

Why doesn't this code position the cursor correctly when I build it with Borland C++?


I found this code for a replacement gotoxy() function using C standard library only. apparently it compiles using GCC and works like the gotoxy() found in conio.h.

However I only have Borland C++ v5.5 compiler, this compiles fine but does not reposition the curser like gotoxy() in conio.h does. Can anybody verify the fact that this works when using GCC or tell me why it doesn't work using Borland?

#include<stdio.h>
#include<stdlib.h>

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

int main()
{
    gotoxy(10, 10);
    printf("hello world");
}

Solution

  • The escape codes being used in your function depend on support being present in your terminal emulator. It may or may not work, depending on the environment you're using. For example, your program works as expected in Mac OS X's Terminal application, running bash in xterm compatibility mode.

    You can read about ANSI escape codes for more information about this specific case, which is the "HVP – Horizontal and Vertical Position" command.