Question: Build a form-letter generator that can prepare a customized document for each record in a database (this is often referred to as a mail-merge feature).
Build a form-letter generator that can prepare a customized document for each record in a database (this is often referred to as a mail-merge feature). Design small schemas and input files to test the correctness of your program. There is skeleton code to help you get started, you only have to insert your code on the missing parts of the skeleton code.
SKELETON CODE
#include
#include
#include
#define INPUT_LENGTH 128
#define FIELD_LENGTH 30
#define NUM_FIELDS 9
int main( int argc, char * argv[] )
{
FILE *template = NULL;
FILE *data = NULL;
char input[INPUT_LENGTH]; // a string for reading from a file
char customerData[NUM_FIELDS][FIELD_LENGTH]; // an array of strings for storing the results of a "split"
// these 3 variables are useful for processing the text input
int element = 0;
char *next;
char ch;
// yes, we're hard-coding file names... note that this is *unacceptable*, unless we explicitly tell you to do it
template = fopen( "template.txt", "r" );
if( NULL != template )
{ // read in the customers until we're done
data = fopen( "data.txt", "r" );
if( NULL != data )
{
while( NULL != fgets( input, INPUT_LENGTH, data ) )
{
// INSERT PARSING CODE HERE!
// You're tokenizing the line and putting the data into the customerData array ala split in Java
// generate the output by reading and parsing the template
// instead of reading it into a buffer it just re-reads the file each time
rewind( template );
while( NULL != fgets( input, INPUT_LENGTH, template ) )
{
// INSERT PARSING CODE HERE!
// Process the input one character at a time looking for variables to replace with customerData
}
}
fclose( data );
}
fclose( template );
}
return EXIT_SUCCESS;
}
DATA.TXT
Public|Jane|Q|Ms.|600|Maple Street|Your Town|Iowa|12345 Penner|Fred|R|Mr.|123|that Street|Winnipeg|MB|R3T 2N2 Gardner|Mark|E|Mr.|76|The Avenue|Toronto|ON|M5W 1E6 Acton|Hilda|P|Mrs.|66|What Blvd|Winnipeg|MB|R3T 2N2
Template.txt
Welcome back, $1! We hope that you and all the members of the $0 family are constantly reminding your neighbours there on $5 to shop with us. As usual, we will ship your order to $3 $1 $2. $0 $4 $5 $6, $7 $8
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
