4

I'm confused as to how the writing processing goes in C. So I have a string, s, that I want to write to the output. To do that, I use fputs:

fputs(s, stdout);

But apparently this does not write to the output, but merely collect the data for writing? Where exactly is it collected? So I have to wait until the program exits or till I call fflush() till the output is actually written into stdout? Am I right?

Jens
  • 69,818
  • 15
  • 125
  • 179
George Newton
  • 3,153
  • 7
  • 34
  • 49
  • 1
    Sometimes output is not flushed until and end of line `\n` is written. – Paulo Bu Feb 27 '14 at 17:59
  • @PauloBu Thats true for printf, not for fputs. – Zaffy Feb 27 '14 at 18:00
  • @Zaffy point taken, I thought it could be similar. – Paulo Bu Feb 27 '14 at 18:01
  • Show a minimal program that does not write the output. – Jabberwocky Feb 27 '14 at 18:03
  • 2
    @zaffy No, the buffering mode is a property of the stream, not of the function writing to the stream. – Jens Feb 27 '14 at 18:04
  • Are you by any chance on windows and used to say `fputs("\nsome text", stdout)` instead of `fputs("some text\n", stdout)`? – Jens Feb 27 '14 at 18:06
  • @GeorgeNewton people are suggesting stuff to you and you are ignoring it. – Paulo Bu Feb 27 '14 at 18:09
  • Which platform are you using ? Under Windows it works fine. Show us your program that "does not work". – Jabberwocky Feb 27 '14 at 18:10
  • My program is working. I'm just asking where the data is collected when fputs is called, and where it is written when fflush is called. I'm sorry if that wasn't clear. – George Newton Feb 27 '14 at 18:12
  • @Jens: Where did that horrible practice come from and why is it so typical on Windows? :-( – R.. GitHub STOP HELPING ICE Feb 27 '14 at 18:22
  • 1
    The data is sort of "collected" via stdout which if of type FILE and which encapsulates a lot of internal bookkeeping stuff. Step through `fputs` with a debugger and you will see how it works. It can be pretty tough though. – Jabberwocky Feb 27 '14 at 18:24
  • @R.. I believe it is due to command.com outputting an extra newline when a command terminates. If you `printf("hello, world\n");` you end up with a blank line before the prompt. To avoid the blank line, programmers started using the newline at the start of a string (but that's only my personal theory). – Jens Feb 27 '14 at 21:16

1 Answers1

3

The C Standard IO streams are operating in one of three modes:

  1. fully buffered
  2. line buffered
  3. unbuffered

You can set the mode with the setvbuf() function. This all happens deep in the guts of the Standard IO implementation. If you want your data to appear immediately, use unbuffered mode.

Quoting from C99 7.19.3#3:

When a stream is unbuffered, characters are intended to appear from the source or at the destination as soon as possible. Otherwise characters may be accumulated and transmitted to or from the host environment as a block. When a stream is fully buffered, characters are intended to be transmitted to or from the host environment as a block when a buffer is filled. When a stream is line buffered, characters are intended to be transmitted to or from the host environment as a block when a new-line character is encountered. Furthermore, characters are intended to be transmitted as a block to the host environment when a buffer is filled, when input is requested on an unbuffered stream, or when input is requested on a line buffered stream that requires the transmission of characters from the host environment. Support for these characteristics is implementation-defined, and may be affected via thesetbufandsetvbuf functions.

Jens
  • 69,818
  • 15
  • 125
  • 179