
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
int fd;
char filename[] = "/home/xuzhengyang/2.c", buf[] = "happy new year!\n";
if ( (fd = open(filename, O_WRONLY)) == -1) {
perror("open");
exit(1);
}
/* duplicate fd file descriptor */
if (dup2(fd, STDOUT_FILENO) == -1) {
perror("dup2");
exit(1);
}
/* output data to fd and STDOUT_FILENO */
if (write(fd, buf, strlen(buf)) != strlen(buf)) {
perror("write");
exit(1);
}
if (write(STDOUT_FILENO, buf, strlen(buf)) != strlen(buf)) {
perror("write");
exit(1);
}
exit(0);
}