dup2函数运用之重定向标准输出
2013-09-11 14:21
309 查看
利用dup2函数进行重定向标准输出,使输出到一个文件中
main.c
/*********************************************************************************
* Copyright: (C) 2013 Yanshifu<Yanshifu@gmail.com>
* All rights reserved.
*
* Filename: main.c
* Description: This file
*
* Version: 1.0.0(09/11/2013~)
* Author: Yan Shifu <Yanshifu@gmail.com>
* ChangeLog: 1, Release initial version on "09/11/2013 01:01:54 PM"
*
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#define pstr "file content"
/********************************************************************************
* Description:
* Input Args:
* Output Args:
* Return Value:
********************************************************************************/
int main (int argc, char **argv)
{
if (argc < 2)
{
printf("Usage: %s filename", argv[1]);
exit(1);
}
print(argv[1], pstr);
return 0;
} /* ----- End of main() ----- */
print.c
/*********************************************************************************
* Copyright: (C) 2013 Yanshifu<Yanshifu@gmail.com>
* All rights reserved.
*
* Filename: print.c
* Description: This file
*
* Version: 1.0.0(09/11/2013~)
* Author: Yan Shifu <Yanshifu@gmail.com>
* ChangeLog: 1, Release initial version on "09/11/2013 01:03:40 PM"
*
********************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void print(const char *filename, const char *str)
{
int fd;
if ((fd = creat(filename, 0666)) < 0)
{
printf("creat file error \n");
exit(1);
}
dup2(fd, STDOUT_FILENO);
printf("str = %s", str);
}
print.h
/********************************************************************************
* Copyright: (C) 2013 Yanshifu<Yanshifu@gmail.com>
* All rights reserved.
*
* Filename: print.h
* Description: This head file
*
* Version: 1.0.0(09/11/2013~)
* Author: Yan Shifu <Yanshifu@gmail.com>
* ChangeLog: 1, Release initial version on "09/11/2013 02:02:54 PM"
*
********************************************************************************/
#ifndef _PRINT_H
#define _PRINT_H
void print(const char *filename, const char *str);
#endif
makefile
bins=main
objs=main.o
srcs=main.c
$(bins):$(objs)
gcc -o main main.o print.o
$(objs):$(srcs)
gcc -c main.c
gcc -c print.c print.h
clean:
rm -f $(bins) *.o *.h.gch
main.c
/*********************************************************************************
* Copyright: (C) 2013 Yanshifu<Yanshifu@gmail.com>
* All rights reserved.
*
* Filename: main.c
* Description: This file
*
* Version: 1.0.0(09/11/2013~)
* Author: Yan Shifu <Yanshifu@gmail.com>
* ChangeLog: 1, Release initial version on "09/11/2013 01:01:54 PM"
*
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#define pstr "file content"
/********************************************************************************
* Description:
* Input Args:
* Output Args:
* Return Value:
********************************************************************************/
int main (int argc, char **argv)
{
if (argc < 2)
{
printf("Usage: %s filename", argv[1]);
exit(1);
}
print(argv[1], pstr);
return 0;
} /* ----- End of main() ----- */
print.c
/*********************************************************************************
* Copyright: (C) 2013 Yanshifu<Yanshifu@gmail.com>
* All rights reserved.
*
* Filename: print.c
* Description: This file
*
* Version: 1.0.0(09/11/2013~)
* Author: Yan Shifu <Yanshifu@gmail.com>
* ChangeLog: 1, Release initial version on "09/11/2013 01:03:40 PM"
*
********************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void print(const char *filename, const char *str)
{
int fd;
if ((fd = creat(filename, 0666)) < 0)
{
printf("creat file error \n");
exit(1);
}
dup2(fd, STDOUT_FILENO);
printf("str = %s", str);
}
print.h
/********************************************************************************
* Copyright: (C) 2013 Yanshifu<Yanshifu@gmail.com>
* All rights reserved.
*
* Filename: print.h
* Description: This head file
*
* Version: 1.0.0(09/11/2013~)
* Author: Yan Shifu <Yanshifu@gmail.com>
* ChangeLog: 1, Release initial version on "09/11/2013 02:02:54 PM"
*
********************************************************************************/
#ifndef _PRINT_H
#define _PRINT_H
void print(const char *filename, const char *str);
#endif
makefile
bins=main
objs=main.o
srcs=main.c
$(bins):$(objs)
gcc -o main main.o print.o
$(objs):$(srcs)
gcc -c main.c
gcc -c print.c print.h
clean:
rm -f $(bins) *.o *.h.gch
相关文章推荐
- APUE之dup,dup2函数重定向标准输出实例
- linux下通过dup2标准输出重定向查看系统进程方法(也是popen函数实现的方法)
- 【Linux调试技巧----标准输出重定向到文件】dup2和dup的妙用
- 用dup2将标准输出重定向到一个打开的文件,稍后,恢复标准输出
- dup和dup2重定向标准输出到文件
- 使用dup2重定向了标准输出后,使用什么方法恢复对终端的输出?? http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=359433&page=
- linux c 使用dup2函数将输出重定向到文件 --execl
- Linux shell 重定向标准错误输出
- (整理)关于C语言中的输入和输出函数的一些运用
- linux下的标准输出和错误输出重定向
- 详解python的几种标准输出重定向方式
- linux的标准输入输出和重定向
- Python输出标准输出重定向
- 在C和C++中把标准输出重定向到指定文件
- [zz]bash标准输出、错误输出、重定向标准输出2>&1
- dup2测试用例 输出重定向
- wxPython 笔记(11)重定向程序输出到标准控制台窗口
- 标准输入输出、错误输出、重定向标准输出
- 如何将标准输出到一个文件重定向从 Visual C/c + + 程序
- 编写读取内存的程序 ——keil uVision3 printf函数重定向到串口输出