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

Write a C code that copies one file from the source file to destination file wit

ID: 3744099 • Letter: W

Question

Write a C code that copies one file from the source file to destination file with two offset values as follows: $./name HW1 source file src off destination file dst off 1. name HW1 program copies the contents of source file from (src off+ 1)th byte to the end of the source file to destination file at (dst off+ 1)th 2. The program should accept following commands S./name HW1 source file destination file S./name HW1 source file src off destination file S./name HW1 source file destination file dst off If the offset option is omitted, by default, offset becomes 0. Other than three commands specified above, the error message should be returned if you try a command such as $./name HW1 src.off source file dst off destination file S./name HW1 source file destination file src off dst off 3. Validation check should be performed for offset values (negative and float offsets are not accepted) 4. If the destination file exists, existing content should be preserved. The contents of the destination file will be kept before and including the dst off). 5. When the specified starting point to copy is greater than the file size of the source file, nothing will be copied (the size of the destination file will be zero if destination does not exit). Tips: Use 'getlnt(... ' or 'getLong(...)' functions to convert the numeric input string to integer defined in tlpi hdr.h header file. You can refer two sample codes (fileio/copy.c and fileio/seek-io.c) covered in the lecture. Compile using 'make name_HW1 (without .)' in the 'fileio' directory given in the source file distribution of the textbook.

Explanation / Answer

To copy the contents of file we have to use the fseek() function:

fseek( FILE *fp, long offset, int pos)

where, FILE *fp: file pointer

offset: specifies offset that is how many number of bytes we have to move

pos: postion that is starting, current and end positions in file.

#include <stdio.h>
#include <stdlib.h>
  
int main()
{
FILE *src, *dest;
char filename[100], c;
  
printf("Source file name: ");
scanf("%s", filename);
  
src = fopen(filename, "r");
if (src == NULL)
{
printf("Cannot open file %s ", filename);
exit(0);
}
  
printf("Destination file name: ");
scanf("%s", filename);
  
  
dest = fopen(filename, "w");
if (dest == NULL)
{
printf("Cannot open file %s ", filename);
exit(0);
}
  
c = fgetc(src); //reading all the characters for source file
while (c != EOF)
{
fputc(c, dest); //putting all the characters into destination file
c = fgetc(src);
}
  
printf(" Source file content copied to Destination %s", filename);
  
fclose(src);
fclose(dest);
return 0;
}

This is for copying the contents of file from source top destination but without offset.

suppose we are copying with offset and supplying arguments in command line we need to change the main mehtod like this: int main(args, char *argv[]) where,

args[1] : source file

args[2]: destination file

args[3]: offset

args[4]: no. of bytes to copy

So instead of direct opening the file we will read file as:

src= fopen(args[1],"r");

    if( fp1 == NULL )

    {

        printf(" %s File can not be opened : ",args[1]);

        return -1;

    }

//for writing contents to dest file

dest= fopen(args[2],"w");

    if( fp2 == NULL )

    {

        printf(" %s File can not be opened ",args[2]);

        return -1;

    }

offset = atoi(args[3]);

no_of_bytes = atoi(args[4]);

and to copy the content with offset we have the code:

fseek(src,offset,SEEK_SET); //SEEK_SET is the begining of file

fread(data,no_of_bytes,1,src); //Read data from source file

fwrite(data,no_of_bytes,1,dest); // Write data to destination file

  

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