davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

how to write into the terminal to lines above by printf()?

Sat Apr 09, 2016 9:12 am

hey,
how can I write into the terminal to lines above using printf() format strings?

e.g., by
"\n" I get a new line below
"\t" is a tab space to the right
"\r" is supposed to be back to the start of the current line ...

but which formatting command do I have to use if I want to write into some former lines, already been written some lines above, to overwrite them?
(My C program is opening the LX Terminal, Raspbian Jessie)
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
jojopi
Posts: 3270
Joined: Tue Oct 11, 2011 8:38 pm

Re: how to write into the terminal to lines above by printf(

Sat Apr 09, 2016 9:51 am

Perhaps you did want to use ncurses after all. It makes this kind of task easier and more portable.

In most terminals, the escape sequence to move the cursor up is the same as that generated by the up arrow key: "\033[A". You can specify a count by adding decimal digits between [ and A. Or to move to a specific location, "\033[y;xH".

For more information, see: http://rtfm.etla.org/xterm/ctlseq.html

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: how to write into the terminal to lines above by printf(

Sat Apr 09, 2016 10:24 am

thank you, I'll try that ASAP!
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: how to write into the terminal to lines above by printf(

Sat Apr 09, 2016 11:53 am

works like a charm!
Thank you very much! :)

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <stdint.h>


//*************************************************************
// main
//*************************************************************

#define cursup    "\033[A"
#define curshome  "\033[0;0H"


int main() {
    char  sbuf[128];
   
     printf("\n");
     printf("1\n");
     printf("2\n");
     printf("3\n");
     printf("4\n");
     printf("5\n");
     printf(cursup);
     printf(cursup);
     printf("4 new");
     printf(curshome);
     printf("0 new");
     
     int c = getchar();
          
     exit(0);
}

#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

Return to “C/C++”