[Solved] C program with functionality as cp command


The open() system call returns a file descriptor, which may not be 1, so your termination condition,

 while(infile==1){

is compeletely bogus. You should test if the read() call read any input (the return value is the number of bytes read, which is zero when it hit end-of-file.) Please read the read man page… pun intended 🙂

What’s more, if (rr = 1) is an assignment which is always true. You should use == for comparisons.

Oh, and exit(-1)/return(-1) is just wrong (a return value at least under Unix is from 0 to 127). Programs indicate failure by returning 1 or EXIT_FAILURE from <stdlib.h>.

2

solved C program with functionality as cp command