CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2005
    Posts
    399

    Post Alternate printf() function

    Hi all,

    I am faced with a tricky issue and would very much like somebody to either give me a solution or some useful pointers.

    I would like to add a feature in my program that will 'bypass' the printf() calls if a particular condition is not met. Now I may have a lot of printf()s all over the program so checking for the condition near each such function is not a feasible option.
    My code should look something like this

    Code:
    PRINT(*parameter*)
    {
    #ifndef CONDITION
    printf(*parameter*)
    #endif
    }
    
    void main()
    {
    PRINT(*anything can go in here*);
    }
    I am open to any other ideas.

    Thanks,
    Leo

  2. #2
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Alternate printf() function

    Yes, it's possible.
    You can write a variadic function, using <stdarg.h> in a C program or <cstdarg> in a C++ program.
    Code:
    #include <stdarg.h>
    
    int print(const char* Format, ...) {
      if (condition) {
        va_list ls;
        int r;
        
        va_start(ls, Format);
        r=vprintf(Format, ls);
        va_end(ls);
        return r;
      }
      return 0;
    }
    Have a look at:
    http://www.cppreference.com/stdio/vp..._vsprintf.html
    PS: "void main()" is not a correct prototype for the main function. You should use "int main()".
    Last edited by SuperKoko; November 10th, 2006 at 05:12 PM.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  3. #3
    Join Date
    May 2005
    Posts
    399

    Re: Alternate printf() function

    Thanks SuperKoko...it works just as expected!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured