Linux: gotoxy() UserPageVisits:1248 active 80 80 DaniWeb 561 60 2010-10-13T19:42:02+00:00

Linux: gotoxy()

Stack Overflow

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.

About the Author

Very fluent in the C coding language. Always up for a challenge, and always learning.

code snippet
#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!

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 () {
	gotoxy(2, 0);
	printf("Coordinates: x = 2; y = 0;\n");
	gotoxy(5, 5);
	printf("Coordinates: x = 5; y = 5;\n");

	return 0;

gotoxy function code is nice:)

but didnt got the logic :(

Be a part of the DaniWeb community

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