Homework Solution: Write a C code that copies one file from the original file to destination file as follows….

    Write a C code that copies one file from the original file to destination file as follows. $ ./copy_HW1 off1 original_file off2 destination_file 1. This usage of command, copy_HW1 shows that copy_HW1 program copies from (off1+1) th byte to the end of the original_file to destination_file starting from (off2+1) th byte. example: $ ./copy_HW1 100 x.txt 50 y.txt : copies contents from 101 st bytes to the end of x.txt file to 51 st byte position of y.txt 2. The number of command arguments should accept 3 or 4 or 5 arguments. $ ./copy_HW1 original_file destination_file copies original_file to destination_file starting from the first position for both files $ ./copy_HW1 off1 original_file destination_file copies original_file starting from (off1+1) th byte to destination_file starting from the first position $ ./copy_HW1 original_file off2 destination_file copies original_file starting from the first byte to destination_file starting from the (off2+1) th byte Other than this, error message should be returned. 3. Invalid offset value (for example -1 for SEEK_SET) should be checked giving an error message such as “Invalid argument”. 4. When the specified starting point is greater than the file size of the original_file, destination_file will have empty content (size 0). 5. Use ‘getLong( ... )’ function defined in tlpi_hdr.h header file 6. Compile using ‘make copy_HW1’ in the ‘fileio’ directory given in the source file distribution of the textbook. 7. Each case (1 through 5 with different arguments) will be tested.

    Expert Answer

     
    Here is the C program that copies content from original file to destination file: #include <stdio.h>

    Write a C legislation that copies individual refine from the coercionmer refine to scope refine as follows.

    $ ./copy_HW1 off1 coercionmer_refine off2 scope_file

    1. This custom of direct, copy_HW1 shows that copy_HW1 program copies from

    (off1+1) th byte to the object of the coercionmer_refine to scope_refine starting from

    (off2+1) th byte.

    example: $ ./copy_HW1 100 x.txt 50 y.txt : copies deviation from 101 st bytes to the

    object of x.txt refine to 51 st byte posture of y.txt

    2. The estimate of direct arguments should sanction 3 or 4 or 5 arguments.

    $ ./copy_HW1 coercionmer_refine scope_file

    copies coercionmer_refine to scope_refine starting from the earliest posture coercion twain refines

    $ ./copy_HW1 off1 coercionmer_refine scope_file

    copies coercionmer_refine starting from (off1+1) th byte to scope_refine starting from the

    earliest posture

    $ ./copy_HW1 coercionmer_refine off2 scope_file

    copies coercionmer_refine starting from the earliest byte to scope_refine starting from the

    (off2+1) th byte

    Other than this, fault communication should be returned.

    3. Invalid offset compute (coercion pattern -1 coercion SEEK_SET) should be checked giving an fault

    communication such as “Invalid argument”.

    4. When the positive starting purpose is superior than the refine extent of the coercionmer_file,

    destination_refine succeed entertain void satisfied (extent 0).

    5. Use ‘getLong( … )’ administration defined in tlpi_hdr.h header refine

    6. Compile using ‘make copy_HW1’ in the ‘fileio’ directory attached in the fountain refine

    distribution of the textbook.

    7. Each instance (1 through 5 with incongruous arguments) succeed be tested.

    Expert Repartee

     

    Here is the C program that copies satisfied from coercionmer refine to scope refine:

    #include <stdio.h>

    #include <stdlib.h>

    int main()

    {

    FILE *fptr1, *fptr2;

    char refinename[100], c;

    printf(“Enter the refinename to notorious coercion lection n”);

    scanf(“%s”, refinename);

    fptr1 = fopen(filename, “r”);

    if (fptr1 == NULL)

    {

    printf(“Cannot notorious refine %s n”, refinename);

    exit(0);

    }

    printf(“Enter the refinename to notorious coercion adaptation n”);

    scanf(“%s”, refinename);

    fptr2 = fopen(filename, “w”);

    if (fptr2 == NULL)

    {

    printf(“Cannot notorious refine %s n”, refinename);

    exit(0);

    }

    c = fgetc(fptr1);

    while (c != EOF)

    {

    fputc(c, fptr2);

    c = fgetc(fptr1);

    }

    printf(“nDeviation copied to %s”, refinename);

    fclose(fptr1);

    fclose(fptr2);

    return 0;

    }