23,118
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define METHOD 1
int main()
{
#if METHOD == 1
setvbuf(stdout, NULL, _IONBF, 0);
#endif
int fd1=open("eup.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
if(fd1==-1){
printf("#LINE %d:",__LINE__);
perror("open");
return -1;
}
/* printf("Hello, World!\n"); */
int fd2=dup2(fd1,1);
if(fd2==-1){
printf("#LINE %d:",__LINE__);
perror("open");
return -1;
}
printf("fd1:%d\nfd2:%d\nfd3:%d\n",fd1,fd2,3);
#if METHOD == 2
char buf[200] = {0};
sprintf(buf, "fd1:%d\nfd2:%d\nfd3:%d\n",fd1,fd2,3);
write(fileno(stdout), buf, strlen(buf)+1);
#endif
#if METHOD == 3
fclose(stdout);
#endif
close(fd2);
close(fd1);
return 0;
}