How to stop printf from printing???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mvairavan
    New Member
    • Jul 2010
    • 1

    #1

    How to stop printf from printing???

    Hi Guys!!!
    I am new to bytes.com and this is my first post, and unfortunately its a weird one :P

    K here is it.. i have a c code which uses some pre-compiled library calls, i just want to stop the printf calls inside that library only after certain point.

    Say like

    void main(){
    //do some stuff

    printf("blah blah..");

    if(condn){
    LIB_call(); //don't print
    }else {
    LIB_call(); //Print
    }
    }

    Well if it was not a precompiled one i could have used

    #define printf if(condn){//}else{printf}

    which would comment out the unwanted printf's, but its a precompiled one :(
    It will be fine even if i can push the outputs to a separate file or something like that, i jus wanna separate them.

    I know its weird, but after all its C, so anything can happen??
    Thanks in advance.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Devices are typically identified in unix as if they were files in directory /dev. For instance, /dev/tty1, /dev/con, etc. There is typically a device called /dev/null which discards characters rather than printing them. You could redirect stdout to /dev/null.

    In your case, you want don't want to redirect the output until your program has been running for awhile. You would need to make appropriate system calls to redirect stdout rather than redirect it from the shell as you start your program. I don't know how to do that, but google is friend.

    If your not using unix then ... I'll have to think about it some more.

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      Under windows, devices have typically have nasty names.

      The historic "null" device name is "NUL:"; I don't know if there's a more formal name under the later versions of windoze.

      Fortunately for the general developer, the "null" device is important enough that it has been around since the days of PC DOS. Likely that explains the simple name we generally see/use in our batch files.

      You will likely use something like the following:

      Code:
      FILE *f = ::freopen("NUL:", "w", stdout);
      if (NULL == f)
        <throw-an-error-here>

      Comment

      Working...