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

Using files, input and output and command line arguments in C Write a C program

ID: 3831391 • Letter: U

Question

Using files, input and output and command line arguments in C Write a C program that takes three file names as command line arguments. The main function must first check to see that the correct number of arguments was passed. If the number of arguments is wrong, the program should print an error message and terminate. If the right number of arguments is passed, the main function must call another function to do the following: Assume that the first file is the output file. The second file contains the text of a letter. In file, the string "#N#" will indicate the full name of a recipient. The string "#A#" will indicate the address of the recipient. The string "#F#" will indicate the first name of the recipient. The string "#L#" will indicate the last name of the recipient. The #_# strings can appear any number of times in the text of the letter. The third file contains the names and addresses of number of recipients. This file is in the following form: Fullname Firstname Lastname Address City, State, Zip Blank line or end of file Your program will open the output file and copy the letter to the output file for each of the recipients in the recipient file, replacing the strings "#N#", "#A#", "#F#" and "HL#" with the information for the recipients found in the recipient files To test your program, I have created files called letter txt and recipients txt Email me your .c file.

Explanation / Answer

#include<stdio.h>

void main (int argc, char **argv) {
     char fullname[50];
     char firstname[50];
     char lastname[50];
     char city[50];
     char state[50];
     char zip[10];
     char input[50];
     int match;
     FILE *fout, *fin1, *fin2;
     if (argc != 4) {
        printf("Provide all the three files ");
        return;
     }
     fout = fopen(argv[1], "w");

     fin2 = fopen(argv[3], "r");

     while (fscanf(fin2, "%s %s %s %s %s %s", fullname, firstname, lastname,city, state, zip) != EOF){
           fin1 = fopen(argv[2], "r");
           while (fscanf(fin1, "%s", input) != EOF){
               match = 0;
               if (input == "#N#") {
                  match = 1;
                  fprintf(fout, "%s", fullname);
               }
               if (input == "#F#") {
                    match = 1;
                    fprintf(fout, "%s", firstname);
               }
               if (input == "#L#") {
                     match = 1;
                     fprintf(fout, "%s", lastname);
                }
                if (input == "#A#") {
                     match = 1;
                     fprintf(fout, "%s %s %s", city, state, zip);
                }
                if (match == 0){
                   fprintf(fout, "%s", input);
                }

            }
            fclose(fin1);
            fprintf (fout, "%s", "---------------------------------------------------- ");
     }
     fclose(fin2);
     fclose(fout);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote