tags:

views:

1404

answers:

5

How can I erase the current line printed on console in C ? I am working on a linux system

example -

printf("hello"); printf("bye");

I want to print bye on the same line in place of hello.

output bye

+6  A: 

You can use a \r (carriage return) to return the cursor to the beginning of the line:

printf("hello");
printf("\rbye");

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:

printf("hello");
printf("\rbye  ");

Or, first erase it with a few spaces, then print your new string:

printf("hello");
printf("\r          ");
printf("\rbye");

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.

Andre Miller
printf("hello"); printf("\rbye\n"); will print byelo i want to print only bye
I did a quick initial reply - I have updated the answer since.
Andre Miller
A: 

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:

printf("fooooo\r");
printf("bar");

the output will be:

barooo

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:

char str[MAX_LENGTH];        
// init str to all spaces, NULL terminated with character as '\r'
strcpy(str, my_string);       // copy my_string into str
str[strlen(my_string)] = ' '; // erase null termination char
str[MAX_LENGTH - 1] = '\r';
printf(str);

You can do error checking so that my_string is always atleast one less in length than str, but you get the basic idea.

Ashwin
+1  A: 

You could delete the line using \b

printf("hello");
int i;
for (i=0; i<80; i++)
{
  printf("\b");
}
printf("bye");
alexander.egger
Doesn't \b just move the cursor back one character? The characters will still be there. I guess you could do something like "\b \b".
Andre Miller
On my Linux box the line gets deleted. Maybe the behavior is different on other platforms.
alexander.egger
+1  A: 

When I was a mere lad, we had a library called "curses" that let you draw all over the screen with characters, because we hadn't invented graphics yet.

I think it had to go into full-screen mode first, but that restriction may have since been lifted. Also, it might not be called 'curses' on Linux.

-Wil

Wil Shipley
It is called, inventively, "ncurses" :)
Juraj
+3  A: 

You can use VT100 escape codes. Most terminals, including xterm, are VT100 aware. For erasing a line, this is ^[[2K. In C this gives:

printf("%c[2K", 27);
mouviciel
This does not reset the cursor to the beginning of the line (in my terminal, at least), so `\r` is still required. Note that the use of `%c` is not necessary. Alternative: `printf("\33[2K\r");`
Stephan202
Just as I had suspected, my solution was a workaround
Ashwin