Thread: reassigning FILE pointers & fprintf

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    reassigning FILE pointers & fprintf

    I want a way to selectively turn on & off file inputs at compile time without commenting out large portions of code. I thought I could do this:

    Code:
    FILE* fileout;
    
    if(NoWrite)  //NoWrite = 1 means no output
    fileout = NULL;
    else if((fileout=fopen("put.out","w"))==NULL)
    printf("Could not open output file!\n");
    
    fprintf(fileout,"writing to output file\n");
    //many more of these, and in other places
    
    fclose(fileout);
    but I get access violations, presumably because you can't pass fprintf a NULL pointer. So what else can I do to disable opening & printing of the output file? The program I'm writing needs to operate off a CD without writing any outputs.

    Thanks!

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Code:
    fileout = stdout;
    .
    .
    .
    if(NoWrite ! = 1)
       fclose(fileout);
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    7
    Cshot, unfortunately I can't write it all to stdout.

    Originally posted by Salem
    Ideally, all your logging functions should go through a separate function, so you can apply your 'NoWrite' test in a single place
    I do. I just didn't feel like posting that much detail of our code.

    So there's a NULL pointer and a NUL device? No mention of NUL in MSVC++'s documentation.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You could use #define to control how sections of code are used at compile time. For example:
    Code:
    #define MY_DEBUG_FLAG
    
    
    FILE *fp;
    #ifdef MY_DEBUG_FLAG
        fp=fopen(....);
        fprintf(fp, "my message");
        fclose(fp);
    #endif
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM