I want to rewrite the "cp" command of Linux. So this program will work like #./a.out originalfile copiedfile
. I can open the file, create new file but can't write the new file. Nothing is written. What could be the reason?
The current C code is:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,char *aa[]){
int fd,fd1;
char buffer[100];
if(argc!=3){
printf("Usage : ./a.out <original> <copy> \n");
return -1;
}
fd=open(aa[1],O_RDONLY,S_IRUSR);
if(fd==-1){
printf("file not found.\n");
return -1;
}
fd1=open(aa[2],O_CREAT | O_WRONLY,S_IRUSR);
if(fd1!=-1){
printf("file is created.\n");
}
ssize_t n;
while(n=read(fd,buffer,50)){
write(fd1,buffer,n);
printf("..writing..\n");
}
close(fd);
close(fd1);
}