Lil' Bits
Lil' Bits

Reputation: 898

Clear stdout in C?

Is there any way to clear the standard output buffer in C?

I have a program in which I don't know if I am going to output data until the end of a loop.

I would like to have my logic be something like:

printf("data1\n");
printf("data2\n");

...

if (printTuple)
    fflush(stdout); //Print the data to stdout
else
    /*Clear stdout*/ //Clear stdout

I have tried memset(stdout, 0, sizeof(stdout)) and rewind(stdout), neither of which work.

Upvotes: 1

Views: 17331

Answers (5)

Utkan Gezer
Utkan Gezer

Reputation: 3069

I couldn't get an answer to my comment, but I think since you are using fflush to print, you seem to have your stdout buffered.

In that case, you must have allocated some memory as a buffer as well. That buffer should have been the memory you should be setting to 0, like memset( stdout->_base, 0, stdout->_bufsiz ), but that wouldn't do the job either.

Anyway, the following line would do, what I think you'd like to have, provided that your situation is how I think it is:

    stdout->_ptr = stdout->_base;

Yeah, simple as that.

I have no idea whether this is undefined behaviour or not; it is just something that I had found out while I was examining the FILE structure a while ago.

Upvotes: 1

Leo Feradero Nugraha
Leo Feradero Nugraha

Reputation: 31

In linux you can use '\033c' code for cleaning stdout example: fputs("\033c", stdout);

Upvotes: 2

Paul R
Paul R

Reputation: 212959

On Linux you can use __fpurge, and on some Unices (BSD, Solaris, et al) you can use fpurge, but this is not portable to other platforms, such as Windows.

#include <stdio.h>
#include <stdio_ext.h>

if (printTuple)
    fflush(stdout);   // Print the data to stdout
else
    __fpurge(stdout); // Clear stdout

As others have already noted, your whole approach is fraught with potential problems and you should really re-think your design.

Upvotes: 2

There is no way to reliably retract what has been committed to printf() and friends. However, you can use open_memstream() to your advantage: It gives you a FILE* to which you can fprintf() anything you want in the normal way. However, once you close the FILE* created with open_memstream(), you get a malloc'd buffer with the entire text you printed to the file pointer.

Usage is as follows:

char* buffer = NULL;
size_t bufferSize = 0;
FILE* myStream = open_memstream(&buffer, &bufferSize);

fprintf(myStream, "You can output anything to myStream, just as you can with stdout.\n");
myComplexPrintFunction(myStream);    //Append something of completely unknown size.

fclose(myStream);    //This will set buffer and bufferSize.
printf("I can do anything with the resulting string now. It is: \"%s\"\n", buffer);
free(buffer);

Upvotes: 3

jwodder
jwodder

Reputation: 57470

Clearing stdout is not an option. Instead, you just have to avoid printing anything to stdout until after you've determined whether that's what you want to do. If your program is simple enough, this could be accomplished by just relocating the printf statements. For more complex programs, you'll need some intermediate storage of what you'll be printing (either the values to print or an internal buffer of the actual strings).

Upvotes: 3

Related Questions