简体   繁体   English

是否可以使用回车 \\r 而不覆盖已经写入的文本?

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

Let's say I have printed Welcome in my console.假设我在控制台中打印了Welcome If I use如果我使用

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

I only get Hello world.我只得到Hello world. Is there any special command to get Hello world. Welcome有没有什么特殊的命令可以获取Hello world. Welcome Hello world. Welcome without printing Hello world and then Welcome. Hello world. Welcome而不打印 Hello world 和 Welcome。 Basically, I want carriage return without overwriting the text.基本上,我想要回车而不覆盖文本。 Is this possible in C?这在C中可能吗?

No, it is not possible in ANSI C. Assuming you want this to be ANSI C, you will have to print "Hello, World."不,这在 ANSI C 中是不可能的。假设您希望这是 ANSI C,您将不得不打印"Hello, World." first and then print " Welcome" .首先,然后打印" Welcome"

Otherwise, there are various solutions, including curses.否则,有各种解决方案,包括诅咒。 See mevets's answer for a curses solution.有关诅咒解决方案,请参阅 mevets 的答案。

First try man curses , then man ncurses if that fails.首先尝试man curses ,然后尝试man ncurses如果失败。 Once you have found the curses documentation, the function you want is insstr , or possibly inch :一旦你找到了 curses 文档,你想要的函数是insstr ,或者可能是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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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