PDA

View Full Version : clrscr() and gotoxy() in gcc



mitchi
September 12th, 2007, 06:49 AM
clrscr()
just add the following to your code to clear the screen first before executing your program.



#include<stdlib.h>
main()
{
system("clear"); //clears the screen
}


gotoxy()
just add the following to your code to use the gotoxy() function, which changes the position of the cursor to the desired coordinates of your screen.



#include<stdio.h>
//gotoxy function
void gotoxy(int x,int y)
{
printf("%c[%d;%df",0x1B,y,x);
}
main ()
{
gotoxy(25,50); //reposition cursor
printf("hello world"); //display text
}

dwhitney67
September 12th, 2007, 07:12 AM
Would it not be easier to use ncurses? All of the "work" has been done to provide one the capability to manipulate the position of the cursor (and output) within an xterm.

$ man ncurses

Lux Perpetua
September 12th, 2007, 08:11 AM
xxxxxxx@xxxx:temp> a.out
hello worldxxxxxxx@xxxx:temp>
Is that what program #2 was supposed to do?

And you really should look into ncurses. It's much more flexible than anything you can hack together with system() and printf().

gnusci
September 12th, 2007, 09:59 AM
I think his two functions are much better than use a library, anyway why two use a library just for two functions... Make a code depending of one library just for a couple of functions does not have any sense...!!!

Maybe the functions are not working completely correct but they will work better after fix some bugs...

Thanks for the functions mitchi...

adeross6
May 28th, 2011, 01:14 AM
clrscr()
just add the following to your code to clear the screen first before executing your program.



gotoxy()
just add the following to your code to use the gotoxy() function, which changes the position of the cursor to the desired coordinates of your screen.

thanks it works, i have tried to this

ziekfiguur
May 28th, 2011, 10:33 AM
Instead of using system to call an external program, you could also use this:


fprintf(stdout, "\033[2J\033[0;0f");
fflush(stdout);

liju.g.chacko
May 25th, 2012, 07:45 PM
TRY THIS ONE

#include<stdio.h>
#include<stdlib.h>
int lasti=0,lastj=0;
void goback(int x,int y)
{
printf("\033[%dA",x);
printf("\033[%dD",y);
}
void gotoxy(int x,int y)
{
extern int lasti, lastj;
goback(lasti, lastj);
int i;
for(i=0;i<x;i++)
printf("\n");
for(i=0;i<y;i++)
printf("\t");
lasti=x;
lastj=y;

}

int main ()
{
int i=0;
while(1)
{ i=i+1;
gotoxy(12,5);
printf("hello i=%d\r",i); //display text
gotoxy(10,5);
printf("hello i-1=%d\r",i-1);
usleep(3*1000);
}
}

ranjanak
May 6th, 2013, 02:32 PM
Hi,
I have tried above code, I am able to change y position in screen but I am not able to change x position, i am passing different x value then then also my x value is 0 only.

ranjanak
May 6th, 2013, 02:52 PM
Hi,
I tried above code but I am not able to move x position, I am passing different value then also x position is 0 only.

nidzo732
May 6th, 2013, 03:37 PM
The gotoxy and clrscr are functions from Windows C library. They are not available on Linux.

matt_symes
May 9th, 2013, 11:39 AM
Old thread so closed.