Write a program that copies the content of a file to another file.Usage: cp file_from file_toif the number of argument is not the correct one, exit with code 97 and print Usage: cp file_from file_to, followed by a new line, on the POSIX standard errorif file_to already exists, truncate itif file_from does not exist, or if you can not read it, exit with code 98 and print Error: Can't read from file NAME_OF_THE_FILE, followed by a new line, on the POSIX standard errorwhere NAME_OF_THE_FILE is the first argument passed to your programif you can not create or if write to file_to fails, exit with code 99 and print Error: Can't write to NAME_OF_THE_FILE, followed by a new line, on the POSIX standard errorwhere NAME_OF_THE_FILE is the second argument passed to your programif you can not close a file descriptor , exit with code 100 and print Error: Can't close fd FD_VALUE, followed by a new line, on the POSIX standard errorwhere FD_VALUE is the value of the file descriptorPermissions of the created file: rw-rw-r--. If the file already exists, do not change the permissionsYou must read 1,024 bytes at a time from the file_from to make less system calls. Use a bufferYou are allowed to use dprintf
Question
Write a program that copies the content of a file to another file.Usage: cp file_from file_toif the number of argument is not the correct one, exit with code 97 and print Usage: cp file_from file_to, followed by a new line, on the POSIX standard errorif file_to already exists, truncate itif file_from does not exist, or if you can not read it, exit with code 98 and print Error: Can't read from file NAME_OF_THE_FILE, followed by a new line, on the POSIX standard errorwhere NAME_OF_THE_FILE is the first argument passed to your programif you can not create or if write to file_to fails, exit with code 99 and print Error: Can't write to NAME_OF_THE_FILE, followed by a new line, on the POSIX standard errorwhere NAME_OF_THE_FILE is the second argument passed to your programif you can not close a file descriptor , exit with code 100 and print Error: Can't close fd FD_VALUE, followed by a new line, on the POSIX standard errorwhere FD_VALUE is the value of the file descriptorPermissions of the created file: rw-rw-r--. If the file already exists, do not change the permissionsYou must read 1,024 bytes at a time from the file_from to make less system calls. Use a bufferYou are allowed to use dprintf
Solution
To solve this problem, we need to write a program that copies the content of one file to another file. The program should be used with the following command line arguments: cp file_from file_to.
First, we need to check if the correct number of arguments is provided. If not, we should exit the program with code 97 and print "Usage: cp file_from file_to" on the POSIX standard error.
Next, we need to check if the file_to already exists. If it does, we should truncate it to ensure we are copying the content to a clean file.
Then, we need to check if the file_from exists and if we have permission to read it. If the file_from does not exist or we cannot read it, we should exit the program with code 98 and print "Error: Can't read from file NAME_OF_THE_FILE" on the POSIX standard error, where NAME_OF_THE_FILE is the first argument passed to the program.
After that, we need to check if we can create or write to the file_to. If we cannot create or write to the file_to, we should exit the program with code 99 and print "Error: Can't write to NAME_OF_THE_FILE" on the POSIX standard error, where NAME_OF_THE_FILE is the second argument passed to the program.
To copy the content of the file_from to the file_to, we should read 1,024 bytes at a time from the file_from using a buffer. This will help us make fewer system calls and improve efficiency.
Finally, we need to ensure that we can close the file descriptors properly. If we cannot close a file descriptor, we should exit the program with code 100 and print "Error: Can't close fd FD_VALUE" on the POSIX standard error, where FD_VALUE is the value of the file descriptor.
The permissions of the created file should be set to rw-rw-r--. If the file already exists, we should not change the permissions.
To achieve this, we can use the dprintf function to print error messages on the POSIX standard error.
Similar Questions
Using File Handling Concept, write aprogram to copy one file to another
What program command saves a copy of a file under a different name?
Copy contents of one file to another fileWrite a Python program that copies the content from one file to another file.Note: Take the input file name from the user.Aim:To create a Python program that reads a text file, copies its contents to another file, and prints the contents to the console.Algorithm:Step 1: Accept a filename input from the user and store it in the variable file.Step 2: Open the input file (file) in read mode and the output file ('OutputData3.txt') in write mode using the open() function.Step 3: Iterate through each line in the input file (fin).Write each line to the output file (fout).Step 4: Close both the input and output files using the close() method.Step 5: Open the output file ('OutputData3.txt') in read mode and store it in the variable fin.Step 6: Iterate through each line in the output file (fin).Print each line to the console.Step 7: Close the output file (fin).Step 8: End the program.Sample Test CasesTest Case 1:Expected Output:Enter·file·name:·InputData1.txtThis·is·this.That·is·this.·That·is·that.·This·is·That.Now·tell·me·what·is·that?And·what·is·this?Confused?Haha!!Test Case 2:Expected Output:Enter·file·name:·InputData2.txtIn·a·coastal·town,·a·lighthouse·stood·tall,holding·a·magical·secret.·The·lighthousewhispered·stories·to·the·sea,·guidinglost·ships·and·providing·comfort·toweary·sailors.
Write a program to copy contents of file ABC.txt to file XYZ.txt.
Mary needs to copy her report.txt file to final_report.txt. Which command will allow her to do this?answercopy report.txt to final_report.txtcopy final_report.txtcopy report.txt final_report.txtcopy final_report.txt report.txt
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.