/* ************************************************************************ * Filename: dup_example.c * Description: * Version: 1.0 * Created: 2011年04月14日 20时13分10秒 * Revision: none * Compiler: gcc * Author: wenhao (wh), hnrain1004@gmail.com * Company: sunplusapp * ************************************************************************/ #include <unistd.h> #include <stdio.h> #include <errno.h> #include <fcntl.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <stdlib.h> #define BUFFER_SIZE 1024 int main(int argc, char *argv[]) { int fd; char buffer[BUFFER_SIZE]; if(argc != 2) { fprintf(stderr,"Usage:%s utfilename/n/a",argv[0]); exit(EXIT_FAILURE); } if((fd=open(argv[1],O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR)) == -1) { fprintf(stderr,"Open %s Error:%s/n/a",argv[1],strerror(errno)); exit(EXIT_FAILURE); } if(dup2(fd,fileno(stdout)) == -1) { fprintf(stderr,"Redirect standard out error:%s/n/a",strerror(errno)); exit(EXIT_FAILURE); } fprintf(stderr,"Now,please input string"); fprintf(stderr,"(to quit use CTRL+D)/n"); while(1) { fgets(buffer,BUFFER_SIZE,stdin); if(feof(stdin)) break; write(fileno(stdout),buffer,strlen(buffer)); } exit(EXIT_SUCCESS); // return 0; }