ttworkhard
ttworkhard

Reputation: 301

How to move terminal cursor with ASCII code?

#include <stdio.h>
int main()
{
    printf("asd");
    char code[4] = { 0x08 ,  0x1b , 0x5b ,0x4b } ; 
    int i = 0 ; 
    while ( i  < 4  )
    {   
        putc(code[i], stdout);
        i++;
    }   
    printf("\n");
    return 0;
}

output:

[root@localhost ~]# ./a.out

as

[root@localhost ~]#

It seems that code "0x08" move terminal cursor left to letter "d" position ,and "0x1b , 0x5b ,0x4b" clear up letter "d" . I want to know the meaning of some codes like { 0x08 , 0x1b , 0x5b ,0x4b } . Is there relevant information available ?

Thanks.

Upvotes: 1

Views: 10918

Answers (2)

konsolebox
konsolebox

Reputation: 75488

See ANSI escape code.

One simple way you can do it is

printf("\e[10C%s\n", "XYZ");

It would move cursor 10 columns to the right and print XYZ:

          XYZ

Upvotes: 7

ttworkhard
ttworkhard

Reputation: 301

another example to support my guess :

#include <stdio.h>
int main()
{
    printf("asd");
    char code[8] = { 0x08 ,  0x1b , 0x5b ,0x4b , 0x08 ,  0x1b , 0x5b ,0x4b } ; 
    int i = 0 ; 
    while ( i  < 8  )
    {   
        putc(code[i], stdout);
        i++;
    }   

    printf("\n");
    return 0;
}

output:

[root@localhost ~]# ./a.out

a

[root@localhost ~]#

Upvotes: 0

Related Questions