Objective The objective of this assignment is to familiarize yourself with the s
ID: 3888887 • Letter: O
Question
Objective
The objective of this assignment is to familiarize yourself with the standard system calls and how to use them in a program.
Assignment: Simple File Copy Program
Write a C/C++ program that only uses only standard system calls to copy the contents of one file to another file. You should only have to use the open(), close(), read() and write() system calls. You can use printf() or fprintf() for error or informational messaging. Your program should not explicitly prompt the user for input/output filenames but rather those should be provided on the command line.
Simple File Copy Program Implementation
The program () is a simple text-based program that takes two arguments from the command line, again no prompting the user from within the program.
1.To start the program
./filecpy <input file> <output file>
where <input file> is the file that is to be copied and <output file> is the file that is copied to. After your program completes you should be able to do a “diff” command on the two files and they should be identical. Last step is to run your program with strace to determine the number of system calls made.
Error Handling
Perform the necessary error checking to ensure that the input file exists and that the output file can be written. You can use the system error “errno” and “strerror” to provide additional error messaging.
Grading
The program will be graded on the basic functionality, error handling and how well the implementation description was followed. Be sure to name your program filecpy.c (no extra characters, capitals) and include the output from the strace command. Note that documentation and style are worth 10% of the assignment's grade!
Submission
The program (source code only) should be posted to cougar courses on the due date.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#define SIZE 9918
int main(int argc, char* argv[]) {
int fd_read, fd_write; //file decriptors for input and output files
ssize_t read_in, read_out; //to store the number of bytes from read and write
char buffer[SIZE]; //char array to store the text...
//checking whether the filenames given correctly..
if(argc != 3){//error handling
printf("%d ",argc);
printf ("You should pass two filenames in command line(with location)...like ./filename file1.txt file2.txt ");
return 1;
}
fd_read = open (argv [1], O_RDONLY);//opening file1 to read
if (fd_read == -1) {//if not open..
perror ("open");//showing error msg
return 2;
}
//opening output file..
fd_write = open(argv[2], O_WRONLY | O_CREAT, 0644);
if(fd_write == -1){//if not open..
perror("open");//showing error msg
return 3;
}
//copying contents of file1 to file2
while((read_in = read (fd_read, &buffer, SIZE)) > 0){
read_out = write (fd_write, &buffer, (ssize_t) read_in);
if(read_out != read_in){
//if not possible..shwoing error
perror("write");
return 4;
}
}
//close file descriptors...
close (fd_read);
close (fd_write);
return (EXIT_SUCCESS);
}
output:
file1:
Johnson 76
Miller 90
Duffy 95
Robinson 45
Ashtony 63
Goofy 89
Pluto 95
Mickey 72
Billy 76
Danny 56
Balto 95
Roony 87
Doc 67
Minne 95
Duckey 72
Andy 76
file2, after executing code
Johnson 76
Miller 90
Duffy 95
Robinson 45
Ashtony 63
Goofy 89
Pluto 95
Mickey 72
Billy 76
Danny 56
Balto 95
Roony 87
Doc 67
Minne 95
Duckey 72
Andy 76
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.