Write a C Program that makes a copy of a file using standard I/O and system calls.

AIM:

Write  a C Program that makes a copy of a file using standard I/O  and system calls.

Program:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

void typefile (char *filename)
{
int fd, nread;
char buf[1024];
fd = open (filename, O_RDONLY);
if (fd == -1) {
perror (filename);
return;
}
while ((nread = read (fd, buf, sizeof (buf))) > 0)
write (1, buf, nread);
close (fd);
}
int
main (int argc, char **argv)
{
int argno;
for (argno = 1; argno < argc; argno  )
typefile (argv[argno]);
exit (0);
}

Output:

student@ubuntu:~$gcc –o prg10.out prg10.c
student@ubuntu:~$cat > ff
hello
hai
student@ubuntu:~$./prg10.out ff
hello
hai