C program how to print in table format alignment
<p>The input is retrieve from text file. Which contain information of </p>

<ul>
<li>product ID</li>
<li>product Name</li>
<li>Product Quantity</li>
<li><p>product Price</p>

<pre><code>1    RAYBAN  1   450.000000 
900 KEYBOARD 100 290.000000
78 MINERALWATER 123 345.000000
2 RAYBAN 2 450.000000
</code></pre></li>
</ul>

<p>After printing the output through command prompt. It was not align with the 1st item. How to make it align with the title of table. As you can see the input of line 1 and 4 almost the same. </p>

<p>Here is the output. </p>

<p><a href="https://i.stack.imgur.com/5MNbN.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5MNbN.jpg" alt="enter image description here"></a></p>

<p>Here is the full code. With <code>gotoxy</code> function. The display function is on </p>

<pre><code>int displayProduct()
</code></pre>

<p>There is a line of code for table titles and also the printf from TXT File. </p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;windows.h&gt;
#include &lt;ctype.h&gt;

void gotoxy(int column, int line);
int main();
int addProduct();
int displayProduct(); //prototype

struct product {
    int quantity, reorder, i, id;
    char name[20];
    float price;
};

COORD coord = { 0, 0 };

void gotoxy(int x, int y) {
    coord.X = x; coord.Y = y;

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

int main() {
    int choice;

    gotoxy(17, 5);
    printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");

    gotoxy(17, 20);
        printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");

    gotoxy(22, 8);
    printf("1. Add Product\n\n");

    gotoxy(22, 10);
    printf("2. Display Product\n\n");

    gotoxy(22, 12);
    printf("3. Search Product\n\n");

    gotoxy(22, 14);
    printf("4. Reorder Level of Product\n\n");

    gotoxy(22, 16);
    printf("5. Update Product\n\n");

    gotoxy(22, 18);
    printf("6. Exit\n\n");

    gotoxy(22, 22);
    printf("Please Enter Your Choice : ");
    scanf(" %d", &amp;choice);

    switch (choice) {
      case 1:
        addProduct();
        break;
      case 2:
        displayProduct();
        break;
      case 3:
        searchProduct();
        break;
      case 4:
        reorderProduct();
        break;
      case 5:
        updateProduct();
        break;
      case 6:
        break;
      default:
        system("cls");
        main();
    }
    return (0);
}

/*MENU CODE ENDS !*/

int addProduct() {
    FILE *fp;

    int i = 0;
    struct product a;
    system("cls");

    fp = fopen("inventory.txt", "a+t");

    char checker;

    do {
        system("cls");

        gotoxy(17, 5);
        printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");

        gotoxy(17, 20);
        printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");

        gotoxy(22, 8);
        printf("Enter product ID : ");
        scanf(" %d", &amp;a.id);

        gotoxy(22, 10);
        printf("Enter product name : ");
        scanf(" %s", a.name);

        gotoxy(22, 12);
        printf("Enter product quantity : ");
        scanf(" %d", &amp;a.quantity);

        gotoxy(22, 14);
        printf("Enter product price : ");
        scanf(" %f", &amp;a.price);

        gotoxy(22, 17);
        fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price); //SAVE TO TXT FILE LINE !
        printf("Record saved!\n\n");

        fclose(fp);

        gotoxy(22, 22);
        printf("Do you want to enter new product? Y / N : ");

        scanf(" %c", &amp;checker);
        checker = toupper(checker);

        i++;

        system("cls");
    } while (checker=='Y');

    if (checker == 'N') {
        main();
    } else {
        do {
            system("cls");

            gotoxy(17, 5);
            printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");

            gotoxy(17, 20);
            printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");

            gotoxy(18, 8);
            printf("&gt;&gt;&gt; Wrong Input! Please Enter Y Or N Only! &lt;&lt;&lt;");

            gotoxy(19, 12);
            printf("Do You Want To Enter New Product? Y / N : ");
            scanf(" %c", &amp;checker);
            checker = toupper(checker);
        } while (checker != 'Y' &amp;&amp; checker != 'N');

        if (checker == 'Y'){
            addProduct();
        }

        if (checker == 'N') {
            system("cls");
            main();
        }
    }
    return(0);
}

/*ADD PRODUCT LINE ENDS !*/

int displayProduct() {
    FILE *fp;
    struct product a;

    char true;
    system("cls");

    fp = fopen("inventory.txt", "r");

    gotoxy(17, 5);
    printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");

    gotoxy(5, 6);
    printf("======================================================================");

    gotoxy(5, 7);
    printf("Product ID\t\t Product Name\t\t Quantity\t Unit Price\n"); //TABLE TITLES !

    gotoxy(5, 8);
    printf("======================================================================");

    gotoxy(5,10);
    while (fscanf(fp, "%d %s %d %f", &amp;a.id, a.name, &amp;a.quantity, &amp;a.price) == 4) {
        printf("%d\t\t\t %s\t\t\t %d\t\t %.2f\n\n", a.id, a.name, a.quantity, a.price); //PRINT FROM TXT FILE TO COMMAND PROMPT.
    }
    fclose(fp);

    printf("\t\t \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");

    printf("\nPress any key to return to Main Menu.");

    getch();

    int main();

    return (0);
}
</code></pre>

<p>Updated one, changes made :  </p>

<pre><code>gotoxy(5,10);
                while(fscanf(fp, "%d %s %d %f", &amp;a.id, a.name, &amp;a.quantity, &amp;a.price)==4)
                {
                printf("%-10d\t\t %-12s\t\t %8d\t %8.2f\n\n", a.id, a.name, a.quantity, a.price);
                }

                fclose(fp);
</code></pre>

<p><a href="https://i.stack.imgur.com/7uLn0.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7uLn0.jpg" alt="enter image description here"></a></p>