首页 > 解决方案 > 是否可以从输出行的中间删除一个字符而不删除它后面的字符?

问题描述

我做了一个小代码示例来向您展示我想要做什么:

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

int ft_putchar(int ch)
{
    char c = (char)ch;
    return (write(1, &c, 1));
}

int main(void)
{
    tgetent(getenv("SHELL"), NULL);
    char * le = tgetstr("le", NULL); // get the termcap for cursor left

    write(1, "Hello world!", 12);
    tputs(le, 1, &ft_putchar); // move the cursor to the left
    write(1, "\b", 1); // erase character
    return (0);
}

所以:如你所见,我正在写“Hello World!” 然后我想要做的是在这个例子中擦除'd'而不删除'!',问题是当我将光标向左移动然后打印'\ b'时,它会删除当前字符但也所有后面的字符,所以最终输出是Hello worl而不是Hello worl!,所以我的问题是:我可以避免这种情况发生还是每次擦除中间的字符后都需要重写结尾?(PS:我知道该tgetent功能不受保护,但仅用于示例)

标签: c

解决方案


推荐阅读