This must be written in C (answered aspa) Your program will first read from two
ID: 3806320 • Letter: T
Question
This must be written in C (answered aspa)
Your program will first read from two files. The first is a comma-separated list of names and addresses (called person.txt), the second is a comma-separated list of birthdates (called dob.txt). There should be two structs that correspond to these two types of values: a Person struct and a Date_of_Birth struct. You will populate these structs with the values from the files and add them to an array as you create them.
Then, sort the array according to the name field using any sorting algorithm you wish. Once the array is sorted, print the data in an organized format to the terminal. There is some sample output below.
Hint: This program may be simpler if you choose to do it using multiple files. A header file containing all of your function prototypes, structures and constants, a driver file which simply open your input and output files and calls your functions, and an implementation file containing all the functions themselves.
Here are some (big) hints for your project:
You may want to consider using these structs based on the input you will see in the files:
typedef struct {
int month;
int day;
int year;
} DATE;
typedef struct {
char* last_name;
DATE* date_of_birth;
int ssn;
char* street_address;
char* city;
int age;
} PERSON;
It may also be helpful to think about dividing this up into 4 separate functions (does not include your main function)
Fill your array
Sort your array
Write output
Free your memory
Sample output (Note the sorted order and the organization of the data including DOB):
Blake 191553 112 Bay Street Lancaster 23
Date of Birth: 8/14/1979
Brown 750977 21 Central Street Ballard Hill 21
Date
of Birth: 5/29/1955
Bush 411499 34 Kennedy Street Littleton 44
Date of Birth: 12/23/1957
Checchi 452759 10 Young Street Sterling 56
Date of Birth: 1/23/1983
Here are the files your reading in from
(dob.txt)
then
(person.txt)
All help is appriciated!
Explanation / Answer
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
/*----------------------------------------------------------------------*/
// structure for date of birth
typedef struct {
int day;
int month;
int year;
} DATE;
/*----------------------------------------------------------------------*/
// structure for person
typedef struct {
char *last_name;
DATE *dob;
int ssn;
char *street_address;
char *city;
int age;
} PERSON;
/*----------------------------------------------------------------------*/
//stores array of records from dob.txt and person.txt
PERSON *person;
//number of lines in a file
int c = 0; // keeps a count
/*----------------------------------------------------------------------*/
//function prototypes
void sort();
void loadFile(FILE*, FILE*);
void printToFile();
void freeMemory();
void numberOfLine(FILE*, FILE*);
/*----------------------------------------------------------------------*/
int main(){
FILE *fptr1, *fptr2;
// opens file for reading
fptr1 = fopen("person.txt","r");
fptr2 = fopen("dob.txt", "r");
// calling the functions
numberOfLine(fptr1, fptr2);
loadFile(fptr1, fptr2);
sort();
printToFile();
// frees the allocated memory
freeMemory();
// closes the files
fclose(fptr1);
fclose(fptr2);
return 0;
}
/*----------------------------------------------------------------------*/
//Function that loads the data
void loadFile(FILE *fptr1, FILE *fptr2){
char str[2000];
char *token;
int i;
//creating person array
person = (PERSON*) malloc((c)*sizeof(PERSON));
for(i = 0; i < c; i++) {
//populating structs
person[i].last_name = (char*) malloc(100);
person[i].street_address = (char*)malloc(100);
person[i].city = (char*)malloc(100);
person[i].dob = (DATE*) malloc(100);
fgets(str, sizeof(str), fptr1);
token = strtok(str,",");
strcpy(person[i].last_name,token);
token = strtok(NULL, ",");
person[i].ssn = atoi(token);
token = strtok(NULL, ",");
strcpy(person[i].street_address,token);
token = strtok(NULL, ",");
strcpy(person[i].city,token);
token = strtok(NULL," ");
person[i].age = atoi(token);
fgets(str, sizeof(str), fptr2);
token = strtok(str,",");
person[i].dob->month = atoi(token);
token = strtok(NULL,",");
person[i].dob->day = atoi(token);
token = strtok(NULL," ");
person[i].dob->year = atoi(token);
}
}
/*---------------------------------------------------------------------*/
// Function that uses bubble sort algorithm to sort data by last name.
void sort(){
int j = 0;
PERSON temp;
for (int i = 0; i < c; i++) {
for (j = (i + 1); j < c; j++) {
if (strcmp(person[i].last_name,person[j].last_name) > 0) {
temp = person[i];
person[i] = person[j];
person[j] = temp;
}
}
}
}
/*---------------------------------------------------------------------*/
// Function for printing data into output.txt file
void printToFile(){
int i;
//creates a file called "output.txt"
FILE *fptr = fopen("output.txt", "w");
for (i = 0; i < c; i++) {
fprintf(fptr," %s %d %s %s %d ",
person[i].last_name, person[i].ssn, person[i].street_address, person[i].city, person[i].age);
fprintf(fptr,"Date of Birth: %d/%d/%d ", person[i].dob->month, person[i].dob->day, person[i].dob->year);
}
//closes file
fclose(fptr);
}
/*---------------------------------------------------------------------*/
// Function for freeing the memory
void freeMemory(){
free(person->last_name);
free(person->street_address);
free(person->city);
free(person->dob);
free(person);
}
/*---------------------------------------------------------------------*/
//Function for checking the number of lines in a file
void numberOfLine(FILE *fptr1, FILE *fptr2){
char ch;
// Checks to see if the file was opened or not.
if(fptr1 == NULL || fptr2 == NULL) {
puts("File did not open! ");
} else {
while (!feof(fptr1)) {
ch = fgetc(fptr1);
if(ch == ' ')
c++;
}
rewind(fptr1); // sets file position to the beginning of the file
}
}
dob.txt
12,3,1967
3,24,1957
2,10,1957
9,26,1952
5,29,1955
12,6,1974
8,14,1979
1,23,1983
7,9,1986
7,4,1989
6,25,1982
10,19,1950
3,2,1962
8,6,1982
5,23,1963
12,23,1957
9,3,1956
person.txt
Wilson,122461,46 Blossom Street,Lowell,50
Meszaros,347006,81 River Street,Ashby,30
Hicks,617432,101 Grant Avenue,Pepperell,31
Chu,724731,233 Maple Street,Leominster,42
Brown,750977,21 Central Street,Ballard Hill,21
Latona,100854,105 University Avenue,Hubbardson,24
Blake,191553,112 Bay Street,Lancaster,23
Checchi,452759,10 Young Street,Sterling,56
Ratta,826660,106 Seagram Drive,Westminster,58
Chen,767944,212 King Street,Boston,65
Gervasoni,633911,200 Queen Street,Cambridge,66
McMillan,230469,345 Hickory Heights Court,Allston,44
Munro,274170,2345 Westmount Road,Lexington,17
Wilder,768189,23 Manitoba Avenue,Arlington,56
Planansky,462402,567 Winnipeg Street,Cambridge,57
Bush,411499,34 Kennedy Street,Littleton,44
Taylor,355591,54 Gardner Street,Worcester,31
sample output
Blake 191553 112 Bay Street Lancaster 23
Date of Birth: 8/14/1979
Brown 750977 21 Central Street Ballard Hill 21
Date of Birth: 5/29/1955
Bush 411499 34 Kennedy Street Littleton 44
Date of Birth: 12/23/1957
Checchi 452759 10 Young Street Sterling 56
Date of Birth: 1/23/1983
Chen 767944 212 King Street Boston 65
Date of Birth: 7/4/1989
Chu 724731 233 Maple Street Leominster 42
Date of Birth: 9/26/1952
Gervasoni 633911 200 Queen Street Cambridge 66
Date of Birth: 6/25/1982
Hicks 617432 101 Grant Avenue Pepperell 31
Date of Birth: 2/10/1957
Latona 100854 105 University Avenue Hubbardson 24
Date of Birth: 12/6/1974
McMillan 230469 345 Hickory Heights Court Allston 44
Date of Birth: 10/19/1950
Meszaros 347006 81 River Street Ashby 30
Date of Birth: 3/24/1957
Munro 274170 2345 Westmount Road Lexington 17
Date of Birth: 3/2/1962
Planansky 462402 567 Winnipeg Street Cambridge 57
Date of Birth: 5/23/1963
Ratta 826660 106 Seagram Drive Westminster 58
Date of Birth: 7/9/1986
Wilder 768189 23 Manitoba Avenue Arlington 56
Date of Birth: 8/6/1982
Wilson 122461 46 Blossom Street Lowell 50
Date of Birth: 12/3/1967
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.