There is a function that terminates the program by filling in debug information in the log file:

void crash(const char *const _fstr, ...) { if ( (_fstr != NULL) && (strlen(_fstr) > 0) ) { FILE *f = fopen(CRASH_LOG_FILE_NAME, "a"); fprintf(f, _fstr, ???); fclose(f); } abort(); } 

I want to ensure that the format string and some number of arguments are passed to the crash() function. Situations are different. For some crashes, it is desirable to add not only a line describing the problem, but also a series of codes, for example, GetLastError() and WSAGetLastError() .

How to do it?

  • Variable number of arguments can be passed, for example, in the form of an array + length of the array. Inside the function, analyze and build the necessary logic. - iluxa1810
  • I do not need to analyze anything; fprintf () should analyze a format string. How to transfer a set of arguments to it? - user294535
  • one
    @ Maxim comma. - Hivemaster
  • one

1 answer 1

You need to use the vfprintf function, and pass an argument of type va_list , which you get in your function something like

 void crash(const char *const _fstr, ...) { va_list ap; va_start(ap, _fstr); ... vfprintf(f,_fstr,ap); ... va_end(ap); 
  • Thank you, figured out. - user294535