1

I know how to print string or chars in C, but i'm wondering how i could modify a string that have already been printed in the screen (like when you install some packages and # fills the |####---->| 50%), without using any other functions than syscalls.

Servus
  • 21
  • 3
  • "printing" on printer or on screen? – i486 Feb 13 '19 at 13:12
  • on screen (in shell). – Servus Feb 13 '19 at 13:14
  • You cannot move the "cursor" once the data is sent to stdout using standard C, without using some API calls or knowing the specifics of your console. However, progress bar can be rendered using the carriage return (`\r`) character. – vgru Feb 13 '19 at 13:14
  • also : [Erase the current printed console line](https://stackoverflow.com/questions/1508490/erase-the-current-printed-console-line) – Sander De Dycker Feb 13 '19 at 13:14
  • 1
    On unix you would want to use a specialized library like curses/ncurses which manages the terminal capabilities for you thru all the termcap database knowledge. On other systems, other libraries... – aka.nice Feb 13 '19 at 13:18

1 Answers1

3

You can use the carriage return:

#include <stdio.h>
#include <unistd.h>

int main()
{
    printf("%s", "Hello, ");
    fflush(stdout);
    sleep(1);
    printf("\r%s\n", "World!");
    return 0;
}
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76