Azibc
Azibc

Reputation: 15

Printing parallel arrays that contain different types of data to a file

so I'm trying to print data from 4 different arrays in this form:

Alex 599659008.00 19701112 2010
Mark 599232832.00 19702315 2015
Ali 59965680.00 197012415 2020

as you can see there are 4 arrays 2 contain integers, 1 float and 1 contain names (which's a multidimensional array) so how can I print them to a file like that form. the arrays are defined like this:

char names[MAXSIZE][MAXSIZE];
int stdID[MAXSIZE];
float phones[MAXSIZE];
int registrationYear[MAXSIZE];

Upvotes: 0

Views: 76

Answers (1)

0___________
0___________

Reputation: 67745

You need structure to agregate the data, not separate arrays.

#define MAXNAME 64

typedef struct
{
    int stdID;
    float phone;
    int registrationYear;
    char name[MAXNAME];
}person_t;

typedef struct
{
    size_t size;
    person_t people[];
}database_t;

void print_database(FILE *fo, const database_t *db)
{
    for(size_t index = 0; index < db -> size; index++)
    {
        fprintf(fo, "%s %.2f %d %d\n", db -> people[index].name, db -> people[index].phone, 
                db -> people[index].stdID, db -> people[index].registrationYear);
    }
}

database_t db = {.size = 3, .people = {
    {.name = "Alex",  .phone = 599659008.00,  .stdID = 19701112, .registrationYear = 2010},
    {.name = "Mark",  .phone = 599232832.00,  .stdID = 19701112, .registrationYear = 2010},
    {.name = "Ali",   .phone = 599659008.00,  .stdID = 19701112, .registrationYear = 2010},
    }};


int main(void)
{
    print_database(stdout, &db);
}

Upvotes: 1

Related Questions