2

I am writing a C program where I am printing to stderr and also using putchar() within the code. I want the output on the console to show all of the stderr and then finally flush the stdout before the program ends. Does anyone know of a method that will stop stdout from flushing when a putchar('\n') occurs?

I suppose i could just do an if statement to make sure it doesn't putchar any newlines but I would prefer some line or lines of code to put at the top of the program to stop all flushing until i say fflush(stdout) at the bottom of the program

2
  • 4
    Why not write to a buffer then write that to stdout at the point you want it to be displayed? Commented Oct 25, 2013 at 16:12
  • Which OS are you using? Commented Oct 25, 2013 at 16:17

4 Answers 4

3

What you're trying to do is horribly fragile. C provides no obligation for an implementation of stdio not to flush output, under any circumstances. Even if you get it to work for you, this behavior will be dependent on not exceeding the buffer size. If you really need this behavior, you should probably buffer the output yourself (possibly writing it to a tmpfile() rather than stdout) then copying it all to stdout as the final step before your program exits.

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't work - writing to a temporary still seems to flush periodically as you copy the temporary to stdout.
1

Run your command from the console using pipeling:

my_command >output.txt 

All output witten to stderr will appear immediately. The stuff written to stdout will go to output.txt.

Comments

0

Windows only. I'm still looking for the Unix solution myself if anyone has it!

Here is a minimal working example for Windows that sends a buffer to stdout without flushing. You can adjust the maximum buffer size before a flush occurs by changing max_buffer, though I imagine there's some upper limit!

#include <windows.h> #include <string.h> int main() { const char* my_buffer = "hello, world!"; HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); int max_buffer = 1000000; int num_remaining = strlen(my_buffer); while (num_remaining) { DWORD num_written = 0; int buffer_size = num_remaining < max_buffer ? num_remaining : max_buffer; int retval = WriteConsoleA(hStdout, my_buffer, buffer_size, &num_written, 0); if (retval == 0 || num_written == 0) { // Handle error } num_remaining -= num_written; if (num_remaining == 0) { break; } my_buffer += num_written; } } 

Comments

0

You can use setvbuf() to fully buffer output to stdout and provide a large enough buffer size for your purpose:

#include <stdio.h> int main() { // issue this call before any output setvbuf(stdout, NULL, _IOFBF, 16384); ... return 0; } 

Output to stderr is unbuffered by default, so it should go to the console immediately. Output to stdout is line buffered by default when attached to the terminal. Setting it to _IOFBF (fully buffered) should prevent putchar('\n') from flushing the pending output.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.