刘镇武简历,滨海房屋出租,星光游乐园铃声
依赖的头文件
#include <unistd.h>
函数定义
int dup(int oldfd); int dup2(int oldfd, int newfd);
函数作用
实战
#include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> void file_redirect() { //先备份现场 int outfd = dup(1); //先做重定向 int fd = open("world", o_wronly|o_creat,0666); //标准输出到重定向fd到对应的文件 dup2(fd, 1); printf("hello linux\n"); //需要来一次刷新 fflush(stdout); //需要恢复1,重新到标准输出 dup2(outfd, 1); printf("hello linux\n"); } int main(int argc, char* argv[]) { file_redirect(); return 0; }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
linux下文本编辑器vim的使用方法(复制、粘贴、替换、行号、撤销、多文件操作)
网友评论