1. (15 marks)
Fill in the missing statements in Boxes 1 and 2 in the C program shown below so as to
implement the following functions:
(a) For the first write (write(1, bufl, 20)), the contents of bufl will be written to myfile;
(b) For the second write (write(1, buf2, 20)), the contents of buf2 will be written to the
standard output.
Notes:
(1) For file descriptors, 0 is the standard input and 1 is the standard output.
(2) int dup(int fd);
DESCRIPTION:
dup() is a "smart" function that can duplicate the file descriptor, "fd", to the lowest-
numbered unused file descriptor in the file descriptor table.
RETURN VALUE:
On success, return the new file descriptor.
On error, -1 is returned, and errno is set to indicate the error.
(3) New variables can be declared if needed.
int main(void)
{
int fd;
char bufl[20]="Go to myfile";
char buf2[20]="Go to stdout";
if ((fd = open("myfile", O_CREAT|O_WRONLY|O_TRUNC, 00644)) < 0)
{
printf("Error: Cannot open file in open()\n");
exit(-1);
}
BOX1
close(fd);
write(1,buf1, 20);
BOX2
write(1, buf2, 20);
return 1;
}