Linux: gotoxy()

Stack Overflow 0 Tallied Votes 2K Views Share

I've heard the curses library can be useful when trying to implement the handy DOS-only tools of gotoxy() and clrscr() using move() and initscr(). Though, there is a way to write your own gotoxy() in the Linux environment. This topic isn't touched often, so I would like to change that. I'll also show an example of how to use clrscr(), but it isn't our main focus.

#include <stdio.h>
#include <string.h>

void clrscr(void) {
	int i;

	for (i = 0; i < 100; i++)
		// A bunch of new lines for now. It's blank, hey!
		putchar('\n');
}

int gotoxy(int x, int y) {
	char essq[100];		// String variable to hold the escape sequence
	char xstr[100];		// Strings to hold the x and y coordinates
	char ystr[100];		// Escape sequences must be built with characters

	/*
	** Convert the screen coordinates to strings
	*/
	sprintf(xstr, "%d", x);
	sprintf(ystr, "%d", y);

	/*
	** Build the escape sequence (vertical move)
	*/
	essq[0] = '\0';
	strcat(essq, "\033[");
	strcat(essq, ystr);

	/*
	** Described in man terminfo as vpa=\E[%p1%dd
	** Vertical position absolute
	*/
	strcat(essq, "d");
	
	/*
	** Horizontal move
	** Horizontal position absolute
	*/
	strcat(essq, "\033[");
	strcat(essq, xstr);
	// Described in man terminfo as hpa=\E[%p1%dG
	strcat(essq, "G");

	/*
	** Execute the escape sequence
	** This will move the cursor to x, y
	*/
	printf("%s", essq);

	return 0;
}

/*
** Example
*/
int main () {
	clrscr();
	gotoxy(2, 0);
	printf("Coordinates: x = 2; y = 0;\n");
	gotoxy(5, 5);
	printf("Coordinates: x = 5; y = 5;\n");

	return 0;
}

Dani AI

Generated

correctly pointed out that the simple DOS-style gotoxy() can be implemented on Linux either by emitting terminal control sequences or—preferably—by using a terminal handling library. For clarity to : the original routine builds and prints CSI (Control Sequence Introducer) sequences that tell a VT100/ANSI-compatible terminal to set a row then a column. That is why the code converts numbers to strings and prints escape sequences. Two common pitfalls are (1) terminals expect 1-based coordinates (first row is 1), while many C APIs use 0-based indexing, and (2) raw escapes are fragile across terminal types and when output is redirected.

A more robust approach uses ncurses (handles capabilities, buffering, resizing, and restores terminal state):

#include <ncurses.h>

int main(void) {
    initscr();
    clear();
    move(0, 2);                /* move(y, x) - y and x are 0-based */
    printw("Coordinates: x = 2; y = 0;");
    move(5, 5);
    printw("Coordinates: x = 5; y = 5;");
    refresh();
    getch();
    endwin();
    return 0;
}

If sticking with escape sequences, add basic checks and use the kernel window size to avoid out-of-bounds moves:

#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>

struct winsize w;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0) {
    /* w.ws_row and w.ws_col are terminal dimensions */
}

Troubleshooting notes: flush stdout after printing escapes, verify the TERM environment and terminfo/termcap if portability is required, and prefer ncurses or tigetstr/tparm/tputs for production code rather than handcrafted escape strings.

sudheer51 0 Newbie Poster

gotoxy function code is nice:)

sudheer51 0 Newbie Poster

but didnt got the logic :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.