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

Objective The objective of this assignment is to familiarize yourself with the s

ID: 3888875 • 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>

int main (int argc, char** argv) {

char c;

FILE* inFile;

FILE* outFile;

char outFileName[256];

inFile = fopen(argv[1], "r");

if( inFile == NULL )

{

printf("Press any key to exit... ");

exit(EXIT_FAILURE);

}

outFile = fopen(argv[2], "w");

if( outFile == NULL )

{

fclose(inFile);

printf("Press any key to exit... ");

exit(EXIT_FAILURE);

}

printf("Copying %s to %s ", argv[1], argv[2]);

while ((c = fgetc(inFile)) != EOF) {

fprintf(outFile, "%c", c);

}

fclose(inFile);

fclose(outFile);

}

"diff" command to check whether the files are identical:

diff file1 file2

Compile it to an executable named copy, and use strace to execute it as follows:

gcc –o copy copy.c

strace ./copy file1 file2