4

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?

3 Answers3

2

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)

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
1

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.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
0

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

Community
  • 1
  • 1
Yu Hao
  • 119,891
  • 44
  • 235
  • 294