Hi,
I need to define a macro in a header file in C++ which is called lots of time in the application (for particular, thus non-negociable, log reasons).
I have created the macro as following:
The code is built and does the job it is intended for. But it opens and closes the file at each call. I want to create the 'filepointer' object, and instantiate it with the fopen() value as above, outside de macro, just before theCode:#pragma once // include statements #define MYMACRO(one, two) {\ FILE *filepointer;\ /* code */\ filepointer = fopen("logfile.txt", "a");\ fprintf(filepointer, "%s", somemessage);\ fclose(filepointer);\ }\statement, so that not to have to open/close each time the macro is called.Code:#define MYMACRO
I have quickly read that this is possible by defining a class in the header, with diffferent possibilities for the instantiation. I this case I think I would also be able to take off(the file will be closed when application exits).Code:fclose()
Therefore, I appreciate your contribution on how to properly implement this requirement.
Thanks!