本实例将输出和错误输出,重定向到文件outlog.txt文件。
dup实现
测试代码:
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/epoll.h>
#define _DEBUG_INFO
#ifdef _DEBUG_INFO
#define DEBUG_INFO(format, ...) printf("%s:%d $$ " format "\n" \
,__func__,__LINE__ \
, ##__VA_ARGS__)
#else
#define DEBUG_INFO(format, ...)
#endif
static char *file_name = "outlog.txt";
int main(int argc, char **argv)
{
int fd,fd1 = 1,fd2 = 2;
fd = open(file_name,O_CREAT | O_TRUNC | O_RDWR,0666);
if(fd1 < 0){
perror("open");
return -1;
}
DEBUG_INFO("test for dup");
close(1);
close(2);
// if((fd1 = fcntl(fd,F_DUPFD,1)) == -1){
if((fd1 = dup(fd)) == -1){
// if((fd1 = dup2(fd,1)) == -1){
perror("1:dup");
return -1;
}
if((fd2 = dup(fd)) == -1){
perror("2:dup");
return -1;
}
dprintf(STDOUT_FILENO,"write STDOUT_FILENO:fd = %d,fd1 = %d,fd2 = %d,open %s ok\n",fd,fd1,fd2,file_name);
dprintf(STDERR_FILENO,"write STDERR_FILENO:fd = %d,fd1 = %d,fd2 = %d,open %s ok\n",fd,fd1,fd2,file_name);
puts("this is test for testing");
printf("this is test for printf\n");
DEBUG_INFO("test for dup");
close(fd);
close(fd1);
close(fd2);
return 0;
}
执行这个代码:并查看outlog.txt的内容;
dup2实现
使用dup2的好处是,不必使用close(1)和close(2)关闭这两个文件描述符,dup2会自动关闭这两个东西。
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/epoll.h>
#define _DEBUG_INFO
#ifdef _DEBUG_INFO
#define DEBUG_INFO(format, ...) printf("%s:%d $$ " format "\n" \
,__func__,__LINE__ \
, ##__VA_ARGS__)
#else
#define DEBUG_INFO(format, ...)
#endif
static char *file_name = "outlog.txt";
int main(int argc, char **argv)
{
int fd,fd1 = 1,fd2 = 2;
fd = open(file_name,O_CREAT | O_TRUNC | O_RDWR,0666);
if(fd1 < 0){
perror("open");
return -1;
}
DEBUG_INFO("test for dup2");
// close(1);
// close(2);
// if((fd1 = fcntl(fd,F_DUPFD,1)) == -1){
if((fd1 = dup2(fd,1)) == -1){
// if((fd1 = dup2(fd,1)) == -1){
perror("1:dup");
return -1;
}
if((fd2 = dup2(fd,2)) == -1){
perror("2:dup");
return -1;
}
dprintf(STDOUT_FILENO,"write STDOUT_FILENO:fd = %d,fd1 = %d,fd2 = %d,open %s ok\n",fd,fd1,fd2,file_name);
dprintf(STDERR_FILENO,"write STDERR_FILENO:fd = %d,fd1 = %d,fd2 = %d,open %s ok\n",fd,fd1,fd2,file_name);
puts("this is test for testing");
printf("this is test for printf\n");
DEBUG_INFO("test for dup2");
close(fd);
close(fd1);
close(fd2);
return 0;
}
执行这个代码:并查看outlog.txt的内容;
fcntl(oldfd,F_DUPFD,start_fd)实验
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/epoll.h>
#define _DEBUG_INFO
#ifdef _DEBUG_INFO
#define DEBUG_INFO(format, ...) printf("%s:%d $$ " format "\n" \
,__func__,__LINE__ \
, ##__VA_ARGS__)
#else
#define DEBUG_INFO(format, ...)
#endif
static char *file_name = "outlog.txt";
int main(int argc, char **argv)
{
int fd,fd1 = 1,fd2 = 2;
fd = open(file_name,O_CREAT | O_TRUNC | O_RDWR,0666);
if(fd1 < 0){
perror("open");
return -1;
}
DEBUG_INFO("test for F_DUPFD");
close(1);
close(2);
if((fd1 = fcntl(fd,F_DUPFD,1)) == -1){
//if((fd1 = dup2(fd,1)) == -1){
// if((fd1 = dup2(fd,1)) == -1){
perror("1:dup");
return -1;
}
if((fd2 = fcntl(fd,F_DUPFD,2)) == -1){
perror("2:dup");
return -1;
}
dprintf(STDOUT_FILENO,"write STDOUT_FILENO:fd = %d,fd1 = %d,fd2 = %d,open %s ok\n",fd,fd1,fd2,file_name);
dprintf(STDERR_FILENO,"write STDERR_FILENO:fd = %d,fd1 = %d,fd2 = %d,open %s ok\n",fd,fd1,fd2,file_name);
puts("this is test for testing");
printf("this is test for printf\n");
DEBUG_INFO("test for F_DUPFD");
close(fd);
close(fd1);
close(fd2);
return 0;
}
执行后,读文件outlog.txt
dup3实验
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/epoll.h>
#define _DEBUG_INFO
#ifdef _DEBUG_INFO
#define DEBUG_INFO(format, ...) printf("%s:%d $$ " format "\n" \
,__func__,__LINE__ \
, ##__VA_ARGS__)
#else
#define DEBUG_INFO(format, ...)
#endif
static char *file_name = "outlog.txt";
int main(int argc, char **argv)
{
int fd,fd1 = 1,fd2 = 2;
fd = open(file_name,O_CREAT | O_TRUNC | O_RDWR,0666);
if(fd1 < 0){
perror("open");
return -1;
}
DEBUG_INFO("test for dup3");
// close(1);
// close(2);
// if((fd1 = fcntl(fd,F_DUPFD,1)) == -1){
//if((fd1 = dup2(fd,1)) == -1){
// if((fd1 = dup2(fd,1)) == -1){
if((fd1 = dup3(fd,1,O_CLOEXEC)) == -1){
perror("1:dup");
return -1;
}
if((fd2 = dup3(fd,2,O_CLOEXEC)) == -1){
perror("2:dup");
return -1;
}
dprintf(fd1,"write STDOUT_FILENO:fd = %d,fd1 = %d,fd2 = %d,open %s ok\n",fd,fd1,fd2,file_name);
dprintf(fd2,"write STDERR_FILENO:fd = %d,fd1 = %d,fd2 = %d,open %s ok\n",fd,fd1,fd2,file_name);
puts("this is test for testing");
printf("this is test for printf\n");
DEBUG_INFO("test for dup3");
close(fd);
close(fd1);
close(fd2);
return 0;
}
执行后,读文件outlog.txt
F_DUPFD_CLOEXEC实现
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/epoll.h>
#define _DEBUG_INFO
#ifdef _DEBUG_INFO
#define DEBUG_INFO(format, ...) printf("%s:%d $$ " format "\n" \
,__func__,__LINE__ \
, ##__VA_ARGS__)
#else
#define DEBUG_INFO(format, ...)
#endif
static char *file_name = "outlog.txt";
int main(int argc, char **argv)
{
int fd,fd1 = 1,fd2 = 2;
fd = open(file_name,O_CREAT | O_TRUNC | O_RDWR,0666);
if(fd1 < 0){
perror("open");
return -1;
}
DEBUG_INFO("test for F_DUPFD_CLOEXEC");
close(1);
close(2);
// if((fd1 = fcntl(fd,F_DUPFD,1)) == -1){
// if((fd1 = dup(fd)) == -1){
// if((fd1 = dup2(fd,1)) == -1){
// if((fd1 = dup3(fd,1,O_CLOEXEC)) == -1){
if((fd1 = fcntl(fd,F_DUPFD_CLOEXEC,1)) == -1){
perror("1:dup");
return -1;
}
if((fd2 = fcntl(fd,F_DUPFD_CLOEXEC,1)) == -1){
perror("2:dup");
return -1;
}
dprintf(fd1,"write STDOUT_FILENO:fd = %d,fd1 = %d,fd2 = %d,open %s ok\n",fd,fd1,fd2,file_name);
dprintf(fd2,"write STDERR_FILENO:fd = %d,fd1 = %d,fd2 = %d,open %s ok\n",fd,fd1,fd2,file_name);
puts("this is test for testing");
printf("this is test for printf\n");
DEBUG_INFO("test for F_DUPFD_CLOEXEC");
close(fd);
close(fd1);
close(fd2);
return 0;
}
执行后,读文件outlog.txt
小结
练习多多,经验多多,知识多多。