Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question on some C code that I saw

Thread Tools

Question on some C code that I saw

Richard Heathfield
Posts: n/a
Peter Nilsson said:
> Richard Heathfield <(E-Mail Removed)> wrote:

>> Where did the OP dig up this code?

> Obviously I don't know for sure, but there's a 7 year old post
> that looks very very similar...
> <>

Yes, you seem to have a point. (And yes, the fflush(fp) is redundantly
superfluous and unnecessary.)

Richard Heathfield <>
Email: -http://www. +rjh@
Google users: <>
"Usenet is a strange place" - dmr 29 July 1999
Reply With Quote
Charlie Gordon
Posts: n/a
"santosh" <(E-Mail Removed)> a écrit dans le message de news:
fhgf0h$bcs$(E-Mail Removed)...
> In article
> <(E-Mail Removed)>,
> Chad <(E-Mail Removed)> wrote on Thursday 15 Nov 2007 8:34 am:
>> Someone had the following
>> #include <stdio.h>
>> int main(int argc, char *argv[])
>> {
>> FILE *fp = NULL;
>> char buffer[8];
>> /* insert argc/argv checking code here */
>> fp = fopen(argv[1], "w");
>> if(fp != NULL)
>> {
>> while(fgets(buffer, sizeof buffer, stdin))
>> {
>> printf("%s", buffer);
>> fprintf(fp, "%s", buffer);
>> }
>> fflush(stdout);
>> fflush(fp);
>> fclose(fp);
>> }
>> else
>> {
>> fprintf(stderr, "Broken tee.\n");
>> }
>> return 0;
>> }
>> Why did they call fllush()? I thought that flush() got called when you
>> called fclose(). Is this a coding error? If not, why call fflush()
>> when reading from stdin.

> When a stream is closed via an explicit call to fclose() or implicitly
> by normal program termination, all it's buffers are flushed. But
> sometimes it is necessary to make sure that the data is written to the
> device immediately. Waiting till the stream is closed, or till the
> program terminates, or till the buffers fill up, may be unacceptable.
> In such situations fflush() can be employed. One alternative is to turn
> off buffering for the stream by using setvbuf(), but this may not be
> quite enough.
> Note that fflush(stdin) is undefined behaviour.
> The program doesn't seem to make a lot of sense to me. What exactly is
> it supposed to accomplish? At a minimum it must remove the null
> character written by fgets() before sending the buffer to 'fp'
> and 'stdout'. Also I think the two calls to fflush() should be within,
> not outside, the while loop. Also it returns "successful" termination
> for regardless of the program's actual outcome.

This program is a simplistic implementation of the unix tee command. It
copies its standard input to its standard output while saving a copy to a
file whose name is passed as a command line argument.
Of course the buffer size is ridiculous, so is the use of printf and fprintf
instead of fputs, not to mention the redundant calls to fflush.

Your remark regarding the "null character written by fgets" is erroneous:
printf does not output the string terminator. But you have inadvertantly
pointed to a real issue with this implementation: it cannot copy NUL bytes
from stdin to stdout (nor to the file), and may even drop entire chunks of
input in this case. Another issue is the inability to handle binary


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
OT: I saw Van Halen Last Night Davey Moore MCSE 26 08-04-2004 10:22 PM
OT: I saw the ISA 2004 sales pitch... Rowdy Yates MCSE 6 07-06-2004 08:45 PM
TEXAS CHAIN SAW MASSACRE (1974) new DVD question Dr.Nick Riviera DVD Video 4 10-17-2003 11:57 AM
Saw the funniest thing today... Sentient Fluid Computer Support 2 07-17-2003 10:37 AM