Chinaunix首页 | 论坛 | 博客
  • 博客访问: 277966
  • 博文数量: 46
  • 博客积分: 2021
  • 博客等级: 大尉
  • 技术积分: 406
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-03 13:00
文章分类

全部博文(46)

文章存档

2011年(1)

2010年(9)

2009年(2)

2007年(13)

2006年(21)

我的朋友

分类: LINUX

2006-09-21 17:02:24

概要

本文解释如何重新从 C 程序, 定向到文件 stdout 再恢复原始 stdout 同一程序中稍后。 C 函数通常用于重定向 stdout 或 stdin 是 freopen()。 将 stdout 重定向到文件称为 FILE.TXT, 使用以下调用:
   freopen( "file.txt", "w", stdout );
该语句导致所有后续输出, 向 stdout, 它通常定向到转到 FILE.TXT 文件。

要返回到显示 (默认 stdout), stdout 使用以下调用:
   freopen( "CON", "w", stdout );
在两个情况, 检查返回值是 freopen() 以确保重定向到实际发生。

下面是一个短程序来演示是 stdout 重定向:
 

示例代码

// Compile options needed: none

#include 
#include 

void main(void)
{
   FILE *stream ;
   if((stream = freopen("file.txt", "w", stdout)) == NULL)
      exit(-1);

   printf("this is stdout output\n");

   stream = freopen("CON", "w", stdout);

   printf("And now back to the console once again\n");
}
假定该 stdout 是向末尾程序控制台重定向到该程序。
 
引自:
阅读(5662) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~