Problem Write a C program using the following functions to i
Problem: Write a C program using the following functions to implement opening and readinf a file of 100 addresses, stores it in an array of structue, and print it out from the stored array in a formatted fashion.
File Format: FirstName LastName StreetName Street1 Street2 City State Zip Phone1 Phone2 Phone3
Key points: Design of strucute,Array of strucute, File operation,Function Design,Parameter Passing, Program Style and Correctness
Functions: 1. load_array()-load the date into an array of structe
2. print_array()- print the array of strucutre in a formated output
Solution
// C code read from file and produce output
#include <stdio.h>
#include <string.h>
// structure holding all the variables
struct employee
{
char firstName[50];
char lastName[50];
char streetName[50];
char street1[50];
char street2[50];
char city[50];
char state[50];
int zip;
int phone1, phone2, phone3;
};
// function to load data into array
void load_array(struct employee emp[])
{
FILE *fptr;
int i;
// open file in read mode
fptr = fopen(\"1.txt\",\"rb\");
// read from file
for(i = 0; i < 100; ++i)
{
fscanf(fptr,\"%s\",emp[i].firstName);
fscanf(fptr,\"%s\",emp[i].lastName);
fscanf(fptr,\"%s\",emp[i].streetName);
fscanf(fptr,\"%s\",emp[i].street1);
fscanf(fptr,\"%s\",emp[i].street2);
fscanf(fptr,\"%s\",emp[i].city);
fscanf(fptr,\"%s\",emp[i].state);
fscanf(fptr,\"%d\",&emp[i].zip );
fscanf(fptr,\"%d\",&emp[i].phone1);
fscanf(fptr,\"%d\",&emp[i].phone2 );
fscanf(fptr,\"%d\",&emp[i].phone3);
}
fclose(fptr);
}
// function to print data
void print_array(struct employee emp[])
{
int i;
printf(\"FIRSTNAME\\tLASTNAME\\tSTREETNAME\\tSTREET1\\tSTREET2\\tCITY\\tSTATE\\tZIP\\tPHONE1\\tPHONE2\\tPHONE3\ \");
for (i = 0; i < 100; ++i)
{
printf(\"%s\\t\",emp[i].firstName);
printf(\"%s\\t\",emp[i].lastName);
printf(\"%s\\t\",emp[i].streetName);
printf(\"%s\\t\",emp[i].street1);
printf(\"%s\\t\",emp[i].street2);
printf(\"%s\\t\",emp[i].city);
printf(\"%s\\t\",emp[i].state);
printf(\"%d\\t\",emp[i].zip );
printf(\"%d\\t\",emp[i].phone1);
printf(\"%d\\t\",emp[i].phone2 );
printf(\"%d\\t\",emp[i].phone3);
printf(\"\ \");
}
}
int main()
{
// array of structure
struct employee emp[10];
// open file and load data into array
load_array(emp);
// print data
print_array(emp);
return 0;
}

