`
yaojingguo
  • 浏览: 212447 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Redirect and restore stdout in C

阅读更多

After a lot of googling, I find the following code. It works.

 

#include <stdio.h>

void f() {
  printf("stdout in f()");
}

main() {
  int fd;
  fpos_t pos;

  printf("stdout, ");

  // redriect
  fflush(stdout);
  fgetpos(stdout, &pos);
  fd = dup(fileno(stdout));
  freopen("stdout.out", "w", stdout);

  f();

  fflush(stdout);
  dup2(fd, fileno(stdout));
  close(fd);
  clearerr(stdout);
  fsetpos(stdout, &pos);        /* for C9X */

  printf("stdout again\n");
}

 You can compile it with gcc. If you save it in a file with .cpp as extenstion name and compile the cpp file with g++, you need to include unistd.h. Add the following additional line.

#include <unistd.h>
分享到:
评论
2 楼 yaojingguo 2010-08-23  
Hi, Liang Liang

Thanks for the information
1 楼 fuliang 2009-12-16  
使用gcc编译.cpp,可以使用-lstdc++选项,这样gcc就可以链接了,比如:gcc -o test.out test.cpp -lstdc++

相关推荐

    Java反编译软件JAD1

    -& - redirect stderr to stdout (Win32 only) All single-word options have three formats: -o - 'reverses' value of an option -o+ - set value to 'true' or 'on' -o- - set value to 'false' or 'off'

    java反编译工具jad 1.5.8g(可以反编译jdk1.5,1.6)

    redirect STDERR to STDOUT (Win32 only) &lt;br&gt;All single-word options have three formats: &lt;br&gt; -o - 'reverses' value of an option -o+ - set value to 'true' or 'on' -o- - set value to 'false...

    jad 最新版java反编译

    jad 最新版java反编译,最好的java反编译工具 Jad v1.5.8g. Usage: jad [option(s)] (s)&gt; Options: -a - generate JVM instructions as comments (annotate) -af - output fully ... -& - redirect STDERR to STDOUT

    jad.exe+readme.txt

    java反编译工具 Jad accepts the following options: -a - annotate the output with JVM bytecodes -af - same as -a, but output fully qualified names when ... -& - redirect STDERR to STDOUT (Win32 only)

    java反编译工具FrontEnd和jad

    java反编译工具 Jad accepts the following options: -a - annotate the output with JVM bytecodes (default: off) -af - same as -a, but output fully qualified... -& - redirect STDERR to STDOUT (Win32 only)

    Visual C++ 编程资源大全(英文源码 表单)

    stdout and stderr into a window(125KB)&lt;END&gt;&lt;br&gt;94,GradientTitleBar.zip This article shows you how to give your Win95/NT4 modeless dialogs a Win98/W2K like gradient title bar.(42KB)&lt;END&gt;&lt;br&gt;95,...

Global site tag (gtag.js) - Google Analytics