c - How to use Variadic macros with fprintf -


Trying to print a log in a file by writing a macro. My macro looks like shown below:

  #define LOG (fmt, ...) {\ FILE * F; \ F = fopen ("output.txt", "a"); \ Fprintf (F, FMT "% s% d", __VA_ARGS __, __ file____, ___LINE__);}   

and I'm planning to call the log in the following format I:

  LOG ("values:% d% d", num1, num2);   

But when I compile, I get an error

  error: before expected expression ????, ??? Token fprintf (F, FMT "% s% d", __VA_ARGS __, __ file__, __ LINE__);}   

Will anyone tell me where I am going wrong?

Firstly, you have to wrap the macro in a du-all loop, so it's correctly expressed as expressions Will be handled by.

  #define LOG (fmt, ...) while {0}   

then you have to make sure that the phone call () Be successful and you close the file after use.

  file * f = fopen ("output.txt", "a"); If (! F) break; // Break works because you are in a loop fclose (f); // also stream flushes   

Then you include a print in the full macro.

  #degine LOG (fmt, ...) \ do {\ FILE * f = fopen ("output.txt", "a"); \ If (! F) \ break; \ Fprintf (f, fmt "% s% d \ n", __VA_ARGS __, __file____, ___LINE__); \ Fclose (f); \} While (0)   

in the call form:

  LOG ("values:% d% d", 4444,55555);   

Where you need to input at least one correct parameter, along with the corresponding flag in the string.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

c++ - Qt::make_shared for creating QSharedPtr as std::make_shared for creating std::shared_ptr -