1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main()
 {
    char name[5][30] = {"Contactless Thermometer","Hand Sanitizers",
                        "Face Mask", "Surgical Mask",
                            "Oxygen Mask"};
    char code[5][3] = {"CT","HS","FM","SM","OM"};
    char donator[5][15] = {"Japan","USA","China","China","Saudi Arabia"};
    int noOfShip[5] = {1,1,2,2,2};
    float qty[5] = {1.2,3.5,120,38,9};
    
    
    FILE *fptr;
    fptr = fopen("donations.txt","w");
    if(fptr == NULL)
    {
        printf("Error!");
        exit(1);
    }
   
    fprintf(fptr,"Name of Supply \t\t");
    fprintf(fptr,"Supply Code \t");
    fprintf(fptr,"Donator \t");
    fprintf(fptr,"No. of Shipment \t");
    fprintf(fptr,"Quantity Received (millions) \n");
    
    for(int i=0;i<5;i++)
    {
        fprintf(fptr,"%s \t",name[i]);
        fprintf(fptr,"%s \t",code[i]);
        fprintf(fptr,"%s \t",donator[i]);
        fprintf(fptr,"%d \t",noOfShip[i]);
        fprintf(fptr,"%.1f \n",qty[i]);
    }
    fclose(fptr);
    printf("file has been created successfully");
    return 0; 
}

This is my output:

Name of Supply          Supply Code    Donator        No. of Shipment        Quantity Received (millions)
Contactless Thermometer         CT     Japan   1      1.2
Hand Sanitizers         HS      USA    1       3.5
Face Mask       FM      China   2      120.0
Surgical Mask   SM      China   2      38.0
Oxygen Mask     OM      Saudi Arabia   2       9.0

Need help to get my informations aligned under its category .

Zakk
  • 1,935
  • 1
  • 6
  • 17
Its Me
  • 21
  • 1
  • 2
    First of all welcome to Stack Overflow. Then please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please [don't post images of text](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question), and don't shout (write in all upper case) at the ones you ask for help (that's considered kind of rude). And please [edit] your question to improve it. – Some programmer dude Jul 24 '22 at 09:02
  • Loop over each of your arrays and find the longest string in each. Then read [man 3 printf](https://man7.org/linux/man-pages/man3/printf.3.html) and read specifically **Format of the format string** and the **Field width** sections. – David C. Rankin Jul 28 '22 at 04:37

1 Answers1

0

Tabs don't work the way you might expect.

When you include a TAB character in a C string, a TAB character is emitted. How this is displayed depends on what's reading the file.

If you cat the file (under Linux) / type (under Windows) to the console, you'll probably find the TAB character will move the cursor to the next column that's a multiple of 8 spaces.

If you load the file into a text editor with tabs set to, say, 2 spaces, the same will happen on multiple-of-2 column boundaries and it'll look quite different.

The solution is not to use TAB at all. It's just a hostage to fortune. You have no control, as the software writer, how the recipient will display the file you generate, or even whether it ignores TABS or treats them as single spaces.

You should instead use printf()-style formatting to ensure your headers and contents end up where you want them.

char fmt_h[] = "%-30s %14s %16s %18s %30s\n"; // Use for headers

char fmt_d[] = "%-30s %14s %16s %18d %30f\n"; // Use for data

I'll not do your entire homework for you, but this ought to help! The first format string is the one you use, with the strings for your headers as parameters; the second, with the actual per-shipment data. I've not bothered to get the widths exactly correct, so fiddle with them until you get something that looks right.

You might want to be a bit less verbose with your headers, by the way, so each line isn't so wide. Also, "donor" would be better English than "donator".

Jon Green
  • 345
  • 2
  • 5