C program to copy contents of a file to another file

Program:

#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
int main(int argc, char * argv[]){
char block[1024];
int in,out,nread;
in = open(argv[1],O_RDONLY);
out = open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
if(in == -1 || out == -1){
printf("Unable to copy file\n");
return 0;
}
while((nread=read(in,block,1024)) > 0)
write(out,block,nread);
printf("File copied.\n");
close(in);
close(out);
return 0;
}

Comments

Popular posts from this blog

Multi-programming with variable number of tasks(MVT) program in C

Multi-programming with fixed number of tasks(MFT) program in C

Linked file allocation program in C.