4 B

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

#include<sys/types.

h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#define FMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
#define OFLAG (O_WRONLY|O_CREAT|O_TRUNC)
#define MAXBUF 4096
void main(int argc,char *argv[])
{
int fd,nread;
char buf[MAXBUF];
char err1[]= "cannot open input file" ;
char err2[]=" cannot duplicate fd";
if((fd=open(argv[1],OFLAG,FMODE))==-1)
{
perror(err1);
}
printf("fd =%d",fd);
if(dup2(fd,STDOUT_FILENO)==-1)
{
perror(err2);
}
while((nread=read(STDIN_FILENO,buf,MAXBUF))>0)
write(STDOUT_FILENO,buf,nread);
}

You might also like