Thread: Silent mode.

  1. #1
    Registered User
    Join Date
    Feb 2005

    Silent mode.

    Hey, I've got a fairly lengthy program that I'm implementing a 'silent mode' option in (i.e when the program is run, it does what it's designed to do without outputting anything to console). The most obvious way I can think of is to set a flag (short int silent) and do an

    if(silent != 0)
    before every function that would output text. This isn't preferable as you can imagine, because that would end up being a lot of extra code that I have a feeling could be avoided.. can any of you offer up any suggestions as to how I could do this differently? Thanks in advanced =)

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Well if you know when you want it to be "silent" and when you want it not to be then you may want to take a look at freopen() to reassign std output, aka stdout, to something other than the monitor.
    Here is the description for freopen:
    FILE *freopen( 
       const char *path,
       const char *mode,
       FILE *stream 
    Path of new file. 
    Type of access permitted. 
    Pointer to FILE structure. 
    Return Value
    Each of these functions returns a pointer to the newly opened
     file. If an error occurs, the original file is closed and the function 
    returns a NULL pointer value.
    The freopen function closes the file currently associated with 
    stream and reassigns stream to the file specified by path. 
    _wfreopen is a wide-character version of _freopen; the path and
     mode arguments to _wfreopen are wide-character strings. 
    _wfreopen and _freopen behave identically otherwise.
    And here is a sample prog that demonstrates its use:
    #include <stdio.h>
    #include <stdlib.h>
    void redirectStream(char*, char*, FILE*);
    void printStream(char*);
    int main(void){
        printf("\nCurrently stdout is the monitor. Redirecting stdout.\n");
        //redirect stdout to a file
        redirectStream("newStream.txt", "a+", stdout);
        printf("These statements are going to the new stdout\n");
        printf("This will appear in our file newStream.txt\n");
        //now we restore stdout and print out our file
        redirectStream("CON", "w", stdout);    
        printf("\nRedirecting stream to monitor. Displaying file contents\n\n");
        return 0;
    void redirectStream(char* fileName, char* mode, FILE* stream){
        freopen(fileName, mode, stream);
    void printStream(char* fileName){
        char line[80];
        FILE* myFile;
        int i = 1;
        myFile = fopen(fileName, "r");
        if(myFile == NULL){
            printf("Error opening file");
        while(fgets(line, 80, myFile) != NULL){
            printf("%d. %s", i++, line);
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. console mode and service mode
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 06-01-2008, 01:42 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. Shortening main
    By pdstatha in forum C Programming
    Replies: 1
    Last Post: 04-03-2002, 04:56 PM
  4. Showing the directory sturcture
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-26-2002, 04:46 PM
  5. free() usage
    By pdstatha in forum C Programming
    Replies: 5
    Last Post: 03-13-2002, 09:28 AM