Write a C code that copies one file from the original file to destination file a
ID: 3883141 • Letter: W
Question
Write a C code that copies one file from the original file to destination file as follows. $./copy_HW1 off 1 original_file off 2 destination_file 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 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. Invalid offset value (for example-1 for SEEK_SET) should be checked giving an error message such as "Invalid argument". When the specified starting point is greater than the file size of the original_file destination_file will have empty content (size 0) Use 'getLong(...)' function defined in tlpi_hdr.h header file Compile using'middotmake copy_HW1' in the 'fileio' directory given in the source file distribution of the textbook. Each case (1 through 5 with different arguments) will be tested.Explanation / Answer
#include void main(int argc,char **argv) { FILE *fp1, *fp2; char ch; int pos; if ((fp1 = fopen(argv[1],"r")) == NULL) { printf(" File cannot be opened"); return; } else { printf(" File opened for copy... "); } fp2 = fopen(argv[2], "w"); fseek(fp1, 0L, SEEK_END); // file pointer at end of file pos = ftell(fp1); fseek(fp1, 0L, SEEK_SET); // file pointer set at start while (pos--) { ch = fgetc(fp1); // copying file character by character fputc(ch, fp2); } fcloseall(); }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.