A lot of programmers have lost all hope after seeing this website. After all, most of the code I post here was found out in the wild, on real, live, production systems that some of you actually maintain. But today I'd like to offer a glimmer of hope: there are actually people who are out there who are fighting the good fight, doing their best to makes sure that code like today's never makes it to production.

You should all be thanking Kirk L. for being one of those guys. He discovered this in a code review of a colleague. The code also explained why there were hundreds of temp files in the working directory came and a huge leakage of file pointers. If you're not well-versed in C++, suffice it to say that while most people would simply use sprintf, our coder felt it better to format strings by writing temporary files to disk and reading them back ...

void printf_to_string( int first, char * fomat, ... )
{
  va_list marker;

  va_start( marker, first );

  // this is required, or it will not work for some reason
  *(char*)*va_arg( marker, char * );

  // string to write to
  // ** MUST BE FIRST AFTER FORMAT IN FUNCTION COMAND LINE **

  {} /* make the variable */
  char * string_write_to = va_arg( marker, char * );
  char temporary_file_name[ 19 ];
  char * temorary_file_name_name ;

  strcpy( temporary_file_name, "thefileXXXX" );

  temorary_file_name_name = _mktemp( temporary_file_name );

  {}

  FILE * FILE1 = fopen( temporary_file_name, "w" );

  vfprintf( FILE1, fomat, marker );

  fclose( FILE1 );

  {} FILE * FILE11 = fopen( temporary_file_name, "r" ); {}

  // 2 is to the end ... it took some experamenting
  // -1 is so we go back and dont get the blank after it
  fseek( FILE11, -1, 2 ); 

  {}
  double bignumberjustincase = ftell( FILE11 );

  fclose( FILE1 );

  FILE * FILE111 = fopen( temporary_file_name, "r" );

  read( _fileno( FILE111 ), string_write_to, (short)bignumberjustincase );

  int stopHere = (long)string_write_to + (int)0;

  stopHere = stopHere + (bignumberjustincase);
  
  if ( stopHere != 0 ) 
  {
    ((char*)stopHere)[ 0 ] = '\0';
    strcpy( string_write_to + int(bignumberjustincase ), "" );  
    va_end( marker );
  }
}
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!