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

Write a C program that uses files and streams to manipulate data. The program wi

ID: 3547684 • Letter: W

Question

Write a C program that uses files and streams to manipulate data.

The program will open three files, two for input and one for output.

It will read strings from the two input files and copy them to the output file in ascending order.


This operation is called a """"""" merge """""""


The names of the three files will be provided at the command line, input files first followed by the output file.


Open all three files. If any fail to open simply print an error message and "exit" the program.

Read a string from each of the two input files, compare the stings and write the smaller to the output file. Now read another string from the appropriate input file and repeat the process until one file becomes emtpy. Now copy the remaining strings from the non-empty file to the output file and close all three files.

NOTES:

            The strings in the two input files must be sorted ascending.

            You may assume that the maximum string length is 30 characters.

You may assume that the strings are stored one per line in the input file.  Write the strings one per line to the output file.

Your program should handle duplicate strings correctly.


Break this program into functions as you like. DO NOT separate this project into multiple files.

It should be contained in a single (.c) file.

Explanation / Answer

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

int main(int argc, char *argv[])
{
    printf("***********Merge************** ");

    if(argc != 4)
    {
        printf("Error : incorrect command line arguments ");
        printf("Please provide two input file names and one output file name when calling the program. Exiting. . . . ");
        return 0;
    }

    FILE *inp1, *inp2, *outp;

    inp1=fopen(argv[1],"r");
    inp2=fopen(argv[2],"r");
    outp=fopen(argv[3],"w");

    char string1[30], string2[30];
    int scan1=1, scan2=1;

    while(!feof(inp1) || !feof(inp2))
    {
        if(!feof(inp1) && !feof(inp2))
        {
            if(scan1==1)
            {
                fscanf(inp1,"%[^ ]",string1);
            }

            if(scan2==1)
            {
                fscanf(inp2,"%[^ ]",string2);
            }

            scan1=0;scan2=0;

            if(strcmp(string1, string2) < 0)
            {
                fprintf(outp,"%s ",string1);
                scan1=1;
            }
            else
            {
                fprintf(outp,"%s ",string2);
                scan2=1;
            }
        }
        else if(!feof(inp1))
        {
            fprintf(outp,"%s ",string2);
            scan2=1;
        }
        else
        {
            fprintf(outp,"%s ",string1);
            scan1=1;
        }
    }

    return 0;
}


The above is the required code.

Comment for any doubts.

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