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?
-
4You should be using [ncurses](http://www.gnu.org/software/ncurses/) – Scotty Bauer Aug 29 '13 at 16:12
-
5Remark: you (correctly) use `'\n'` not `10`, you should also use `'\b'` not `8` – gx_ Aug 29 '13 at 16:15
3 Answers
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)

- 39,972
- 7
- 52
- 94
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.

- 208,859
- 35
- 376
- 711
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