简体   繁体   中英

If printf(“%c%c”,'A',8); deletes A, why can't printf(“%c%c”,'\n',8); delete new line? How can I do it?

How can I delete a new line character once printed in a C code? I want to write a bunch of lines and delete them and after a pause print some other lines then delete them...in a loop. Like a real time update without scrolling. I can print characters and delete them by printing backspace character but once I print new line, I can't delete the line created. Is there any way to achieve this?

The backspace character '\\b' (ASCII 8) moves to the previous position within the line .

If you are under xterm or vt100 compatible you can make use of console codes :

#include <stdio.h>
#include <unistd.h> /* for sleep() */

int main(void)
{
    printf("Line\n");
    sleep(2);
    printf("\033[A"); /* move cursor one line up */
    printf("\033[K"); /* delete line */
    return 0;
}

As an alternative you can take a look to ncurses (Unix) or conio2 (Windows / MINGW)

The backspace character does not "remove" anything. It's just a character like anything else, in terms of the bytestream/file contents. However, when printed to a terminal, backspace moves the cursor position one unit to the left, allowing the next printed character to replace it on the screen. On most terminals, it does nothing if you're already at the left-most position, but even if it did work there, it would not know where to move to on the previous line.

0x08 , or \\b in ASCII, is just a (special)character that is sent to the stdout . How stdout handles it is up to implementation.

Reference: Usage of \\b and \\r in C

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