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 undivided polish from the pristine polish to aim polish as follows.

    $ ./copy_HW1 off1 pristine_polish off2 aim_file

    1. This action of bid, copy_HW1 shows that copy_HW1 program copies from

    (off1+1) th byte to the object of the pristine_polish to aim_polish starting from

    (off2+1) th byte.

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

    object of x.txt polish to 51 st byte aspect of y.txt

    2. The reckon of bid arguments should recognize 3 or 4 or 5 arguments.

    $ ./copy_HW1 pristine_polish aim_file

    copies pristine_polish to aim_polish starting from the primary aspect control twain polishs

    $ ./copy_HW1 off1 pristine_polish aim_file

    copies pristine_polish starting from (off1+1) th byte to aim_polish starting from the

    primary aspect

    $ ./copy_HW1 pristine_polish off2 aim_file

    copies pristine_polish starting from the primary byte to aim_polish starting from the

    (off2+1) th byte

    Other than this, deception missive should be returned.

    3. Invalid offset appreciate (control pattern -1 control SEEK_SET) should be checked giving an deception

    missive such as “Invalid argument”.

    4. When the fixed starting summit is senior than the polish extent of the pristine_file,

    destination_polish accomplish keep void achieveing (extent 0).

    5. Use ‘getLong( … )’ employment defined in tlpi_hdr.h header polish

    6. Compile using ‘make copy_HW1’ in the ‘fileio’ directory abandoned in the fountain polish

    distribution of the textbook.

    7. Each event (1 through 5 with divergent arguments) accomplish be tested.

    Expert Counter-argument

     

    Here is the C program that copies achieveing from pristine polish to aim polish:

    #include <stdio.h>

    #include <stdlib.h>

    int ocean()

    {

    FILE *fptr1, *fptr2;

    char polishname[100], c;

    printf(“Enter the polishname to known control lection n”);

    scanf(“%s”, polishname);

    fptr1 = fopen(filename, “r”);

    if (fptr1 == NULL)

    {

    printf(“Cannot known polish %s n”, polishname);

    exit(0);

    }

    printf(“Enter the polishname to known control fitness n”);

    scanf(“%s”, polishname);

    fptr2 = fopen(filename, “w”);

    if (fptr2 == NULL)

    {

    printf(“Cannot known polish %s n”, polishname);

    exit(0);

    }

    c = fgetc(fptr1);

    while (c != EOF)

    {

    fputc(c, fptr2);

    c = fgetc(fptr1);

    }

    printf(“nSolution copied to %s”, polishname);

    fclose(fptr1);

    fclose(fptr2);

    return 0;

    }