views:

2103

answers:

6

I'm interested in clearing the output of a C program produced with printf statements, multiple lines long.

My initial guess was to use

 printf("output1\n");
 printf("output2\n");
 rewind(stdout);
 printf("output3\n");
 printf("output4\n");

but this produces

 output1
 output2
 output3
 output4

I was hoping it would produce

 output3
 output4

Does anyone know how to get the latter result?

+3  A: 

One way, is to do a exec('clear').

Amit
+11  A: 

Once you print something to the terminal you can't easily remove it. You can clear the screen but exactly how to do that depends on the terminal type, and clearing the screen will remove all of the text on the screen not just what you printed.

If you really want fine control over the screen output use a library like ncurses.

Craig
I second this answer. ncurses is an excellent library.
yodaj007
+1  A: 

In fact, when you capture/redirect your stdout (./program > output.file), there is no way how to remove contents of that file, even printf("\033[2J\033[1;1H"); just adds this sequence of characters into it.

Yossarian
+4  A: 

Most terminals support ANSI escape codes. You can use a J (with parameter 2) to clear the screen and an H (with parameters 1,1) to reset the cursor to the top-left:

printf("\033[2J\033[1;1H");

Alternatively, a more portable solution would be to use a library such as ncurses, which abstracts away the terminal-specific details.

Adam Rosenfield
+4  A: 

As far as C is concerned, stdout is nothing more than a byte stream. That stream could be attached to a CRT (or flat screen), or it could be attached to a hardcopy device like a teletype or even a sheet-fed printer. Calling rewind on the stream won't necessarily be reflected on the output device, because it may not make any sense in context of that device; think about what rewinding would mean on a hardcopy terminal or a sheet-fed printer.

C does not offer any built-in support for display management, so you'll have to use a third-party library like ncurses.

John Bode
+3  A: 

You can have the desired result both for terminal and pipes if you remember to remove the control characters as well. This is hardcoded for two lines.

#include <stdio.h>

int
main ()
{
    fputs("output1\n",stdout);
    fputs("output2\n",stdout);
    fputs("\033[A\033[2K\033[A\033[2K",stdout);
    rewind(stdout);
    ftruncate(1,0); /* you probably want this as well */
    fputs("output3\n",stdout);
    fputs("output4\n",stdout);
    return 0;
}
jbcreix
wow! that works great? can you please explain the mysterious fputs("\033[A\033[2K\033[A\033[2K",stdout);and ftruncate(1,0); /* you probably want this as well */?
ldog
As I said it's two lines, you can keep track of the new lines if you can, or else clear the screen as suggested by others. clear Google for "VT100 ANSI escape sequences". if you don't truncate the file is rewinded but the data is still there until you overwrite it. eg. "Hello world" rewind "I am God" would become "I am Godrld".
jbcreix
\033[A is go up a line\033[2K is clear that line(else it would have the same problem as rewind)
jbcreix
are you like a linux guru? lol, your like the only person that knows about this out of 6 people...
ldog