title: What is the difference between fprintf and vfprintf in C++?
body: <p>I haven't been able to find an answer to the following question, and I am having some issues related to the functions.</p>
  
  <p>My main programming is done in C#, and never really learned C++ while studying, but in my current job I have to do some C++ programming aswell.</p>
  
  <p>Most of the C++ programming have been done by a former employee, and he made a function for logging.</p>
  
  <p>Once in a while this function results in an error (Access Violation) - this is not shown to the user, but I see it while running the code through debugger. </p>
  
  <p>When the error happens it points to this line of code:</p>
  
  <pre><code>vfprintf( LogFile, fmt, va );
  </code></pre>
  
  <p>I then took a closer look at the code before and after, and to put the above into a context the code around is:</p>
  
  <pre><code>void FileLog( char *fmt, ... )
  {
    va_list       va;
    struct  time  t;
    struct  date  d;
    long          clk;
    static int    ReEntrant = 0;
  
    if( FileLogEnabled == false )
      return;
  
    ReEntrant++;
    if( ReEntrant &gt; 1 )
      return;
  
    if( LogFile == NULL )
      LogFile = fopen( LogFileName, "a+" );
    if( LogFile != NULL )
    {
      gettime( &amp;t );
      getdate( &amp;d );
      fprintf( LogFile, "\n%d-%02d-%02d %2d:%02d:%02d.%02d0&gt; ", d.da_year, d.da_mon, d.da_day, t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund );
  
      va_start( va, fmt );
      vfprintf( LogFile, fmt, va );
      va_end( va );
  
      fflush( LogFile );
      ...
    }
    ReEntrant = 0;
  }
  </code></pre>
  
  <p>Actually I dont understand why it is needed (and if it is?) call both fprintf and then vfprintf? I would think the first fprintf call would write the formatted string to the stream (File), and that would be enough?</p>
  
  <p>A little explanation or some information would be much appreciated :)</p>
  
  <p><strong>EDIT:</strong> After the comment from nos - I tracked down the specific call to the function which today have often been causing this error. </p>
  
  <pre><code>FileLog( "TimerRestore[%d], Name=%s", Package.CurGame-&gt;Timers[ Index ].Name.c_str() );
  </code></pre>
  
  <p>I indeed think this could cause some trouble, as "TimerRestore[%d], Name=%s" should be followed by a decimal and string arguemtn, however only a string argument is given. I need to do some testing, but Im sure the author who wrote this code meant to write:</p>
  
  <pre><code>FileLog( "TimerRestore[%d], Name=%s", Index, Package.CurGame-&gt;Timers[ Index ].Name.c_str() );
  </code></pre>
  
  <p>However I still do not understand why the function call does not always seem to result in an error. Or maybe it is cause of the "ReEntrant" variable in FileLog function is blocking it when it does not fail?</p>
  
  <p>Thanks a lot for all the feedback and information.</p>