C Programming Assignment Objectives: To gain experience in dynamic memory alloca
ID: 3694985 • Letter: C
Question
C Programming Assignment
Objectives: To gain experience in dynamic memory allocation and file processing
Assignment: Write a modularized program that’s reads a file with unknown number of records
Read a file with the following fields: first and last names, address, city, state, and a phone number. All fields are separated by a tab (‘ ’).
Sort records by city and output sorted records to a file
Clearly label the output Well document your code (comments)
NO goto, continue, break (except for switch)
Sample Input:
Mary.......Jones..........6201 Winnetka Ave..........Woodland Hills.............Ca ..................91371................8187196478
(NOTE: THE DOTS ABOVE ARE THE SPACES)
Use adequate test data to test your program. Copy/paste the output into your source code using comments. Be sure your output contains user prompts and what was entered by the user in addition to the results of your program processing.
Explanation / Answer
Completed code related to reading person information from a file
#include <stdio.h>
#include <string.h>
typedef struct {
char last_name[50];
char first_name[50];
char address[300];
char city[30];
char state[30];
char phone[15];
} person_record;
void parse(char* str, person_record *record) {
const char s[2] =" ";
char *token;
int i = 0;
/* get the first token */
token = strtok(str, s);
/* walk through other tokens */
while( token != NULL && i <= 5)
{
//printf( " %s ", token );
if(i == 0) //firstanem
strcpy( record->first_name,token);
else if(i==1)
strcpy( record->last_name,token);
else if(i==2)
strcpy(record->address,token);
else if(i==3)
strcpy(record->city,token);
else if(i==4)
strcpy(record->state,token);
else if(i==5)
strcpy( record->phone,token);
i++;
token = strtok(NULL, s);
}
//return record;
}
int readFile(char* fileName, person_record records[]) {
FILE *inputFile;
inputFile = fopen(fileName, "r");
//printf("reading file ");
int line_size = 254; //change this size accordig to your need
char str[line_size];
if (inputFile == NULL) {
printf("File not found ");
return -1;
}
//char** curArr = NULL;
int count = 0;
person_record *record;
while( fgets (str, line_size, inputFile) != NULL )
{
record = (person_record*) malloc( sizeof(person_record));
parse(str, record);
if(record != NULL) {
records[count++] = *record;
}
}
fclose(inputFile);
return count;
}
int main() {
person_record records[50];
//change file location here
char *filename ="personInfo.txt";
int count = readFile(filename, records);
int i = 0;
printf("printing array - %d ", count);
printf("First Name Last Name Addres City State Phone number ");
for( i =0; i < count; i++) {
printf(" %s %s %s %s %s %s ",records[i].first_name, records[i].last_name, records[i].address,
records[i].city,records[i].state,records[i].phone );
}
}
---output--------------------
printing array - 2
First Name Last Name Addres City State Phone number
Mary Jones 6201, Winnetka Ave, Woodland Hills SanFranciso Ca 8187196478
John Kerry 1234,abcd, jvm hills NewYork NewYork 8439634567
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.