dup与dup2函数
来源:互联网 发布:手机windows预览体验 编辑:程序博客网 时间:2024/06/06 09:00
彻底理解dup(...)和dup2(...)这两个函数的作用
dup函数的作用:复制一个现有的句柄,产生一个与“源句柄特性”完全一样的新句柄(也即生成一个新的句柄号,并关联到同一个设备)
dup2函数的作用:复制一个现有的句柄到另一个句柄上,目标句柄的特性与“源句柄特性”完全一样(也即首先关闭目标句柄,与设备断连,接着从源句柄完全拷贝复制到目标句柄)
dup和dup2都是系统服务,window平台对应DuplicateHandle函数
/* DUP.C: This program uses the variable old to save
* the original stdout. It then opens a new file named
* new and forces stdout to refer to it. Finally, it
* restores stdout to its original state.
*/
#i nclude <io.h>
#i nclude <stdlib.h>
#i nclude <stdio.h>
void main( void )
{
int old;
FILE *new;
old = _dup( 1 ); /* "old" now refers to "stdout" */
/* Note: file handle 1 == "stdout" */
if( old == -1 )
{
perror( "_dup( 1 ) failure" );
exit( 1 );
}
write( old, "This goes to stdout first\r\n", 27 );
if( ( new = fopen( "data", "w" ) ) == NULL )
{
puts( "Can't open file 'data'\n" );
exit( 1 );
}
/* stdout now refers to file "data" */
if( -1 == _dup2( _fileno( new ), 1 ) )
{
perror( "Can't _dup2 stdout" );
exit( 1 );
}
puts( "This goes to file 'data'\r\n" );
/* Flush stdout stream buffer so it goes to correct file */
fflush( stdout );
fclose( new );
/* Restore original stdout */
_dup2( old, 1 );
puts( "This goes to stdout\n" );
puts( "The file 'data' contains:" );
system( "type data" );
}
Output
This goes to stdout first
This goes to file 'data'
This goes to stdout
The file 'data' contains:
This goes to file 'data'
- dup与dup2函数
- dup与dup2函数
- dup与dup2函数
- 函数 dup 与 dup2
- dup与dup2函数介绍
- dup与dup2函数介绍
- dup与dup2函数详解
- dup与dup2函数介绍----转载
- dup和dup2函数
- dup()、dup2()函数
- dup和dup2函数
- dup,dup2函数
- dup,dup2函数
- linux dup()/dup2()函数
- dup()和dup2()函数
- dup和dup2函数
- dup、dup2函数
- dup,dup2函数
- GBin1分享:jQuery新手技巧之“避免过多使用$('.someclass')”
- 生命中最伟大的发明
- POJ1067学习 威佐夫博奕(Wythoff Game)
- Java 实现MD5编码
- 大数相乘
- dup与dup2函数
- 一个简单计算器的c程序
- java 实现JSP页面上一页及下一页封装
- thunderbird3.1的安装并添加消息指示器替换evolution(Ubuntu10.04)的技巧
- Linux配置JDK环境变量
- 成功解决火狐浏览器启动慢的一些办法
- 构建linux下IDE环境--Eclipse篇
- C语言堆栈实现( 动态数组 )
- Ubuntu下配置SSH服务