简体   繁体   中英

Is it possible to use carriage return \r without overwriting already written text?

Let's say I have printed Welcome in my console. If I use

printf(" Welcome");
printf("\rHello world.");

I only get Hello world. Is there any special command to get Hello world. Welcome Hello world. Welcome without printing Hello world and then Welcome. Basically, I want carriage return without overwriting the text. Is this possible in C?

No, it is not possible in ANSI C. Assuming you want this to be ANSI C, you will have to print "Hello, World." first and then print " Welcome" .

Otherwise, there are various solutions, including curses. See mevets's answer for a curses solution.

First try man curses , then man ncurses if that fails. Once you have found the curses documentation, the function you want is insstr , or possibly inch :

These routines insert a character string (as many characters as will fit on the line) before the character under the cursor. All characters to the right of the cursor are shifted right with the possibility of the rightmost characters on the line being lost

#include <ncurses.h>
#include <unistd.h>

int main() {
    initscr();
    printw("Welcome");
    refresh();
    sleep(2);
    insstr("\rHello, world. ");
    refresh();
    sleep(10);
    endwin();
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM