Question
C programming Purpose: Understand how to manipulate files by using system calls of open(), stat(), lseek(), write(), and create(). Instruction: Write a C program (
C programming
Purpose:
Understand how to manipulate files by using system calls of open(), stat(), lseek(), write(), and create().
Instruction:
Write a C program (partcopy) that allows a user to extract some part of an existing file (fileSource) and copy it to a new file fileTarget. The user can specify the number of bytes (num1) from the beginning offilesource, how many bytes (num2) that will be extracted from it, and the new fileTarget name.
>partcopy num1 num2 filesource fileTarget
For instance, if the executable name is partcopy, extract 500 bytes from data.dat starting from the beginning of the file:
>partcopy 0 500 data.dat target.dat
For instance, if the executable name is partcopy, extract 300 bytes from data.dat starting at 50 bytes from the beginning of the file to target.dat:
>partcopy 50 300 data.dat target.dat
Note:
- You need to use the command line argument to acquire the num1, num2, and filetarget name. ( 5pts)
- You need to use open() system call to open an existing file. Print out the error message if thefilesourcedoes not exist by using perror() function. (5 pts)
- You need to check if the size of filesource against the sizes of num1 and num2. Print out the warning message if the file size is smaller than num1+ num2. In this case, your program will just copy the content up to the end of the source file. This is the situation that the file size is too small to copy the requested size of the content. (10 pts)
- Once the filesource is open, use lseek() system call to move the file pointer to the proper location for starting reading: num1 from the beginning of the filesource file. ( 20 pts)
- Create a new file fileTarget and use read() and write() system call to copy the content from filesourcetofileTarget for num2 bytes. Close files when the extraction process is complete. You should test your program for the cases when num!+num2 > file size; num1+num2 < file size and num1 > file size (20 pts)
The thing to Submit:
- A c source code partcopy.c with header, error checking including the max size of file content you can copy, and documentation as comments in the program. If num1+num2 is greater than the size of filesource, your program may only copy up to the end of the file.
- Testing results with screenshots to prove a working copy of the executable according to the instruction. You testing cases should include file size that can handle num1+num2 and cannot handle num1+num2.
The system call manual can also be accessed from the following links other than eve machine:
- http://codewiki.wikidot.com/c:system-calls:write (with examples)
- http://linux.die.net/man/2/lseek
- http://man7.org/linux/man-pages/man2/open.2.html
The usage of stat() function to retrieve a file size is:
#includestruct stat st; stat(filename, &st); size = st.st_size;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started