Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

-Write a code in C language -That has 3 files.(input, output,compile) -Data has

ID: 3839878 • Letter: #

Question

-Write a code in C language -That has 3 files.(input, output,compile) -Data has 4 lines 4 columns -Strings are allowed -Please add notes to what each set of codes does ******* INPUT the address ****** - user input program
Assume that you run a business and keep a computerized file of all of your customers. The address file is formatted as follows

Ms.Peggy Marcher Sterling Enterprise Rentals 888 E.Circle Detroit,MI 54301 President George Bush 1 Whitehouse Boulevard Washington,DC 2001
Assume that each entry for a customer takes exactly four lines. A separate file contains the body of a very polite "Bill Overdue" letter that asks for the customers to pay as soon as possible. Write a program that prompts the user for a customer's name and the current dates; searches the address file for the customer's name; and, if successful, generates an output file that contains an individualized letter with the customer's name and address together in the header of the document with the body of the "Bill Overdue" letter. The output file should be similar to the below:
Ms. Peggy Marches Sterling Enterprise Rentals 888 E. Circle Detroit, MI 54301

Date:5/11/2017
Dear Ms. Peggy Marches,
You're bill is overdue you must pay soon before it goes to collections etc. Lab Assignment 9: Assume that you run a business and keep a computerized file of all of your customers. The address file is formatted as follows: Ms. Peggy Marcher Sterling Enterprise Rentals 888 E. Circle Detroit, MI 54301 Mr. Rudolph Valentino Hollywood Silverware 21 Hollywood Ave. Hollywood, CA 10202 .Presid nt Ge Bush Assume that each entry for a customer takes exactly four lines. A separate file contains the body of a very polite "Bill Overdue" letter that asks for the customer to pay up as soon as possible Write a program that prompts the user for a customer's name and the current d the address file for the customer's name; and, if successful, generates an output file that contains an individualized letter with the customer's name and address together in the header of the document with the body of "Bill Overdue" letter. The output file should be similar to the the below: President George Bush 1 Whitehouse Boulevard Washington, DC 2001 Date: 11/24/2016 Dear President George Bush, You're bill is overdue... (the rest of the letter)...

Explanation / Answer

// C code

#include<stdio.h>
#include<string.h>

char* readAddress(char* name) { //search for an name, if found return addtess, else NULL

FILE *fp = fopen("addressFile.txt", "r");
char * line = NULL, *out;
size_t len = 0;
ssize_t read;
out = malloc(sizeof(char) * 250);
out[0] = '';
while ((read = getline(&line, &len, fp)) != -1) { //read one line at a time
strcpy(out,line);
line[strlen(line) - 2] = ''; //this eats up the last endline character ' ' from the string
if(strcmp(line,name)==0) { //if name matches
getline(&line, &len, fp); //read remaining 3 lines and concat them
strcat(out,line);
getline(&line, &len, fp);
strcat(out,line);
getline(&line, &len, fp);
strcat(out,line);
return out;
}
else { //if not found
getline(&line, &len, fp); //consume more 3 lines
getline(&line, &len, fp);
getline(&line, &len, fp);
}
}
return NULL; //nam not found
}
char* getName(char *address) { //method to extract the name (first line) from the address
char* name = malloc(sizeof(char)*60);
int i = 0;
for (;address[i] != ' '; i++)
name[i] = address[i];
name[i] = '';
return name;
}

void writeLetter(char* address) { //method to write the lettrer to file letter,txt
FILE *fp = fopen("letter.txt", "w");
fprintf(fp,"%s ",address);
fprintf(fp," Date: 11/24/2014 ");
fprintf(fp,"Dear %s, ", getName(address));
fprintf(fp,"Your bill is due... (the rest of the letter) ...");
fclose(fp);
}

void main() {
char name[50], *address;
printf("Enter name: ");
gets(name);
address = readAddress(name);
if (address!=NULL) {
writeLetter(address);
printf("letter printed to file letter.txt");
}
else
printf("Name not found");
}