Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7856491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:43:51+00:00 2026-06-02T20:43:51+00:00

In my C++-code there are several fprintf-statements, which I have used for debugging. Since

  • 0

In my C++-code there are several fprintf-statements, which I have used for debugging. Since I might need them again, I would prefer not to comment them out for the moment.
However, I need the execution of the program to be fast, so I would like to avoid them being printed out, as they are for the moment (I redirected stderr to a file).

Preferably this would be determined by the user passing an argument to the program, which I would extract like this:

main (int argc, char *argv[])
{
int isPrint=0;
if (argc > 1 ) {
       isPrint = atoi ( argv[2]);
}

}

I thought of renaming fprintf to another name, and then from that function do a fprintf-call using the same parameters, based on the value of isPrint; however, then I realized that fprintf can have so many different kind of arguments and a various number of arguments; and that I don’t know any generic way of declaring my own function with those requirements.

So I wonder how to create a function,which works exactly like fprintf, but which takes the extra parameter isPrint; or how to solve the above problem in another way.

Complementary information after first post:
One solution would be to add this before each fprintf-statement:

if (isPrint == true )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:43:52+00:00Added an answer on June 2, 2026 at 8:43 pm

    First note that if this is just for debugging, I’d agree that the typical way is to use macros or preprocessor defines to tell the compiler to include logging or not.

    However, if you don’t want it removed entirely by the compiler (so that you can turn the printing on or off with an argument), you could write your own log function that takes isPrint and some string, and then use snprintf() to format the string before you call it.

    Something along these lines:

    void myLog(int isPrint, char *message)
    {
        if(isPrint == 1)
        {
            fprintf(logFile, "%s", message);
        }
    }
    
    char msg[64];
    snprintf(msg, 64, "Test Message %d", 10);
    myLog(isPrint, msg);
    

    It may also be possible to wrap fprintf() in your own varags function, but that would be more complicated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the source code of SIMBL Agent, there're several codes which send Apple events
I have a legacy ASP.NET/VB.NET WebSite. There several App_Code modules/code files that use types
There are several PHP or js code formatting libs out there -- does anyone
I don't think I need to post my code (there's lots of it) but
I have ace editor integrated in my site. I have some code there, and
In our code base there is a function which should return either an object
I'm working with some code. There are several queries whose effect is, if the
I'm reading through some old code at work, and have noticed that there are
I have several classes that are basically interfaces to database rows. Since the class
I have problem with initializing TabHost. I need to have in View several Tabs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.