locked
vc++ 2015 fprint / fprintf right-hand sided .. RRS feed

  • Question

  • Maybe a simple problem: Visual C++ 2015 printf gives right-hand sided column on the screen (as it should be), while fprintf centers (!) all double values in the column. All left-hand format and 0 filled format work properly.

    How could I get with fprintf a right-hand sided column .. ?

     

    Here a simple code to demonstrate:

     

    int main()

    {

                    int row;

                    double bb;

                    FILE *a;

                    a = fopen("c:\\users\\joe\\desktop\\TEST100_out.txt", "w+");

                    for (row = 0; row < 30; row++)

                    {

                             bb = 12345678.90;

                             if (row > 10)

                            {

                                    bb = 5678.90;

                             }

                             printf ("%15.2f\n", bb);                 // right-hand sided

     fprintf (a, "%15.2f\n", bb);           // centered, but should be right-hand !!!

                    }

                    fclose(a);

    return 0;

    }

    Saturday, August 29, 2015 11:27 AM

Answers

  • In VC 2013 Express it produces right justified text for both as expected.

    Is the tool you use to examine the output file using a monospace font?

    • Marked as answer by Shu 2017 Monday, September 7, 2015 9:09 AM
    Saturday, August 29, 2015 6:20 PM
  • Visual C++ 2015 printf gives right-hand sided column on the screen (as it should be), while fprintf centers (!) all double values in the column.

    Further to Barry's suggestion, what do you see when you run this extended version
    of your program?

    #include <stdio.h>
    
    int main()
     {
        int row;
        double bb;
        FILE *a;
    
        a = fopen("TEST100_out.txt", "w+");
        if(!a)
            {
            printf("File open for writing failed!\n");        
            return -1;
            }
    
        for (row = 0; row < 30; row++)
            {
            bb = 12345678.90;
            if (row > 10)
                {
                bb = 5678.90;
                }
            printf ("%15.2f\n", bb);
            fprintf (a, "%15.2f\n", bb);
            }
        fclose(a);
    
        a = fopen("TEST100_out.txt", "r");
        if(!a)
            {
            printf("File open for reading failed!\n");
            return -1;
            }
    
        char buf[100] = {0};
        while(fgets(buf, sizeof buf, a))
            {
            printf("%s", buf);
            }
        fclose(a);
    
        return 0;
    }

    - Wayne



    • Edited by WayneAKing Saturday, August 29, 2015 10:35 PM
    • Marked as answer by Shu 2017 Monday, September 7, 2015 9:09 AM
    Saturday, August 29, 2015 8:32 PM
  • Visual C++ 2015 printf gives right-hand sided column on the screen (as it should be), while fprintf centers (!) all double values in the column. All left-hand format and 0 filled format work properly.


    Additionally as per Barry's suggestion -

    When the output file is displayed using Total Commander's internal viewer, the display
    shows as follows when "ANSI (Windows charset)" is used:

    But when "Variable char width" is set it shows like this:

    If this corresponds to what you're seeing, then Barry's suggestion is the probable cause.

    Reason: In a non-fixed pitch font the space character is often not as wide as numeric
    characters, so a string of spaces will not be as long physically (or wide) as a string
    of the same number of zeros, ones, etc.

    - Wayne

    • Marked as answer by Shu 2017 Monday, September 7, 2015 9:09 AM
    Saturday, August 29, 2015 11:24 PM

All replies

  • In VC 2013 Express it produces right justified text for both as expected.

    Is the tool you use to examine the output file using a monospace font?

    • Marked as answer by Shu 2017 Monday, September 7, 2015 9:09 AM
    Saturday, August 29, 2015 6:20 PM
  • Visual C++ 2015 printf gives right-hand sided column on the screen (as it should be), while fprintf centers (!) all double values in the column.

    Further to Barry's suggestion, what do you see when you run this extended version
    of your program?

    #include <stdio.h>
    
    int main()
     {
        int row;
        double bb;
        FILE *a;
    
        a = fopen("TEST100_out.txt", "w+");
        if(!a)
            {
            printf("File open for writing failed!\n");        
            return -1;
            }
    
        for (row = 0; row < 30; row++)
            {
            bb = 12345678.90;
            if (row > 10)
                {
                bb = 5678.90;
                }
            printf ("%15.2f\n", bb);
            fprintf (a, "%15.2f\n", bb);
            }
        fclose(a);
    
        a = fopen("TEST100_out.txt", "r");
        if(!a)
            {
            printf("File open for reading failed!\n");
            return -1;
            }
    
        char buf[100] = {0};
        while(fgets(buf, sizeof buf, a))
            {
            printf("%s", buf);
            }
        fclose(a);
    
        return 0;
    }

    - Wayne



    • Edited by WayneAKing Saturday, August 29, 2015 10:35 PM
    • Marked as answer by Shu 2017 Monday, September 7, 2015 9:09 AM
    Saturday, August 29, 2015 8:32 PM
  • Visual C++ 2015 printf gives right-hand sided column on the screen (as it should be), while fprintf centers (!) all double values in the column. All left-hand format and 0 filled format work properly.


    Additionally as per Barry's suggestion -

    When the output file is displayed using Total Commander's internal viewer, the display
    shows as follows when "ANSI (Windows charset)" is used:

    But when "Variable char width" is set it shows like this:

    If this corresponds to what you're seeing, then Barry's suggestion is the probable cause.

    Reason: In a non-fixed pitch font the space character is often not as wide as numeric
    characters, so a string of spaces will not be as long physically (or wide) as a string
    of the same number of zeros, ones, etc.

    - Wayne

    • Marked as answer by Shu 2017 Monday, September 7, 2015 9:09 AM
    Saturday, August 29, 2015 11:24 PM