How can I erase the current printed console line in C? I am working on a Linux system. For example -
printf("hello");
printf("bye");
I want to print bye on the same line in place of hello.
How can I erase the current printed console line in C? I am working on a Linux system. For example -
printf("hello");
printf("bye");
I want to print bye on the same line in place of hello.
You could delete the line using \b
there is a simple trick you can work here but it need preparation before you print, you have to put what ever you wants to print in a variable and then print so you will know the length to remove the string.here is an example.
Some worthwhile subtleties...
\33[2K
erases the entire line your cursor is currently on\033[A
moves your cursor up one line, but in the same column i.e. not to the start of the line\r
brings your cursor to the beginning of the line (r is for rewind) but does not erase anythingIn xterm specifically, I tried the replies mentioned above and the only way I found to erase the line and start again at the beginning is the sequence (from the comment above posted by @Stephan202 as well as @vlp and @mantal)
\33[2K\r
On an implementation note, to get it to work properly for example in a countdown scenario since I wasn't using a new line character
'\n'
at the end of eachfprintf()
, so I had tofflush()
the stream each time (to give you some context, I started xterm using a fork on a linux machine without redirecting stdout, I was just writing to the buffered FILE pointerfdfile
with a non-blocking file descriptor I had sitting on the pseudo terminal address which in my case was/dev/pts/21
):Note that I used both the \33[2K sequence to erase the line followed by the
\r
rewind sequence to reposition the cursor at the beginning of the line. I had tofflush()
after eachfprintf()
because I don't have a new line character at the end'\n'
. The same result without needing fflush() would require the additional sequence to go up a line:Note that if you have something on the line immediately above the line you want to write on, it will get over-written with the first fprintf(). You would have to leave an extra line above to allow for the first movement up one line:
Usually when you have a '\r' at the end of the string, only carriage return is printed without any newline. If you have the following:
the output will be:
One thing I can suggest (maybe a workaround) is to have a NULL terminated fixed size string that is initialized to all space characters, ending in a '\r' (every time before printing), and then use strcpy to copy your string into it (without the newline), so every subsequent print will overwrite the previous string. Something like this:
You can do error checking so that
my_string
is always atleast one less in length thanstr
, but you get the basic idea.This script is hardcoded for your example.
Click here for source.
You can use a
\r
(carriage return) to return the cursor to the beginning of the line:This will print bye on the same line. It won't erase the existing characters though, and because bye is shorter than hello, you will end up with byelo. To erase it you can make your new print longer to overwrite the extra characters:
Or, first erase it with a few spaces, then print your new string:
That will print hello, then go to the beginning of the line and overwrite it with spaces, then go back to the beginning again and print bye.