Search code examples
cc++builderborland-c++

fprintf produces different output


I compiled the next program in C++ Builder 3 and C++ Builder 5, it produces a different output:

#include <vcl.h>
#include <stdio.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
  const char filename[] = "C://fprintf-test.txt";
  FILE *file = fopen(filename, "a");

  fprintf(file, "%0*.0f\n", 7,  99999.00);
  fprintf(file, "%0*.0f\n", 7, -99999.00);
  fprintf(file, "%0*.0f\n", 7, -999.00);
  fprintf(file, "%0*.0f\n", 7, 999999.00);
  fprintf(file, "%0*.0f\n", 7, 9.00);

  return 0;
}

Output in C++ Builder 3:

0099999
-0099999
-0000999
0999999
0000009

Output in C++ Builder 5:

0099999
-099999
-000999
0999999
0000009

You can clearly see that the padding of 0 of the negative values differs. Why is this? Is this documented?

Thanks in advance


Solution

  • As @chux stated in the comments: this is probably a C++ Builder 3 bug. It is not documented.

    If anyone has this problem, we fixed it like this:

    fprintf(FileOut, "%0*.*f\n",
    #if ( __BORLANDC__ >= 0x560 )
            paddingNumber,
    #else
            paddingNumber < 0 ? paddingNumber - 1 : paddingNumber,
            paddingNumber )