Nivaar Exam Prep

25-Comp-A4 Program Design and Data Structures · December 2016

Question 4 of 9: File I/O — Personalized Mail Merge

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer) · Last updated

Notes on this paper

Pseudocode or any high-level language is accepted, and the examiner's note states explicitly that marking emphasises the operation of the program, not syntactic details.

Reference texts for this subject.

  • Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 4th ed. — tree traversals and BSTs (ch. 12), sorting (ch. 2, 7), asymptotic analysis (ch. 3).
  • Weiss, Data Structures and Algorithm Analysis in C, 2nd ed. — linked lists and queues (ch. 3), binary search trees (ch. 4).
  • Deitel & Deitel, C++ How to Program, 10th ed. — class design and templates (ch. 9–12), file streams (ch. 14), operator overloading (ch. 11).
  • Kernighan & Ritchie, The C Programming Language, 2nd ed. — character/array I/O idioms (ch. 1, 7), pointers, structures and linked lists (ch. 5–6).
  • Stroustrup, The C++ Programming Language, 4th ed. — class templates and value semantics (ch. 3, 25–27).

The Computer Engineering citation list is built around architecture and networking texts (Patterson & Hennessy, Tanenbaum, Mano); this subject is programming and data structures, so the works above are cited instead.

Question 4: File I/O — Personalized Mail Merge (20 marks)

Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.

Given. A template text file containing zero or more occurrences of the literal marker #N#, and a recipient name typed at the keyboard.

Find. A copy of the template, written to a second file, with every occurrence of #N# replaced by the typed name.

Approach. Stream the template character by character; whenever the next three characters match #N# exactly, write the name instead and skip past all three marker characters, otherwise copy the character through unchanged.

  1. Write the program. A small 3-character lookahead buffer (implemented here with ungetc, so no separate buffering scheme is needed) lets the marker be recognised without reading the whole file into memory.
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        FILE *in, *out;
        char name[80];
        int c1, c2, c3;
    
        in = fopen("letter_template.txt", "r");
        out = fopen("letter_out.txt", "w");
        printf("Enter recipient's first name: ");
        fgets(name, sizeof name, stdin);
        name[strcspn(name, "\n")] = '\0';        /* strip trailing newline */
    
        c1 = fgetc(in);
        while (c1 != EOF) {
            if (c1 == '#') {
                c2 = fgetc(in);
                if (c2 == 'N') {
                    c3 = fgetc(in);
                    if (c3 == '#') {
                        fputs(name, out);         /* marker matched: substitute */
                        c1 = fgetc(in);
                        continue;
                    }
                    fputc(c1, out); fputc(c2, out);
                    if (c3 != EOF) ungetc(c3, in);
                    c1 = fgetc(in);
                    continue;
                }
                fputc(c1, out);
                if (c2 != EOF) ungetc(c2, in);
                c1 = fgetc(in);
                continue;
            }
            fputc(c1, out);
            c1 = fgetc(in);
        }
        fclose(in);
        fclose(out);
        return 0;
    }
  2. Confirm on a short worked example. Template "Dear #N#, welcome #N#!" with name Chris streams through unchanged up to the first #, matches the full #N# marker there, substitutes Chris, continues copying ", welcome " unchanged, matches the marker a second time, and substitutes again — confirming the "may occur more than once" requirement is met without any assumption that there is exactly one marker. $$\boxed{\text{"Dear \#N\#, welcome \#N\#!"} \to \text{"Dear Chris, welcome Chris!"}}$$
Question 4 — results
TemplateOutput (name = "Chris")
Dear #N#, welcome #N#!Dear Chris, welcome Chris!