Thread: Is there a "garbage" stream?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    8

    Is there a "garbage" stream?

    Hi,

    Here's my problem. I have C code that does a bunch of number crunching, spitting out intermediate results to the output files along the way. However, in some applications, it would be preferable to NOT write any data to output files (e.g., when the program is run iteratively in an nonlinear optimization routine).

    Rather than hunt through my code to find all the instances of fprintf(fpout, ...) and insert a conditional statement, it would be much easier (and cleaner) if I could simply initialize the file pointer to a stream going nowhere.

    I've tried setting fpout = NULL but that doesn't work. Any suggestions?

    Scott

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7.19.4.3 The tmpfile function

    Synopsis
    1 #include <stdio.h>
    FILE *tmpfile(void);


    Description

    2 The tmpfile function creates a temporary binary file that is different from any other existing file and that will automatically be removed when it is closed or at program termination. If the program terminates abnormally, whether an open temporary file is removed is implementation-defined. The file is opened for update with "wb+" mode.

    Recommended practice
    It should be possible to open at least TMP_MAX temporary files during the lifetime of the program (this limit may be shared with tmpnam) and there should be no limit on the number simultaneously open other than this limit and any limit on the number of open files (FOPEN_MAX).

    Returns
    3 The tmpfile function returns a pointer to the stream of the file that it created. If the file cannot be created, the tmpfile function returns a null pointer.
    Last edited by Dave_Sinkula; 11-07-2002 at 03:56 PM.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    8
    Originally posted by Salem
    Which OS are you using, and which compiler?

    Do you start with
    fpout = fopen( "somefile", "w" );
    OS = Win2K
    Compiler = MingW (gcc)

    Yes, that's exactly the syntax.

    Scott

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    8
    Thanks for the info on tmpfile()--hadn't seen that before. It does the job, though I'm concerned the size of the temp file could get unruly before it goes away.

    Scott

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    8
    Thanks to all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New stream flushing FAQ candidate?
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 07-01-2009, 05:57 PM
  2. Discard wrong data from stream
    By mesmer in forum C Programming
    Replies: 7
    Last Post: 11-16-2008, 02:30 AM
  3. Closing a stream
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2008, 05:15 PM
  4. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM
  5. Buffering??
    By Brain Cell in forum C Programming
    Replies: 15
    Last Post: 09-21-2004, 06:57 AM