extern FILE *stdout 重新定向 和 STDOUT_FILENO

2121阅读 0评论2010-10-16 bo_00
分类:C/C++

首先要知道 FILE *stdout 和 STDOUT_FILENO 的区别。
如下:


参考stdin(3)的man手册:

Name

stdinstdoutstderr - standard I/O streams

Synopsis

#include <>
extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;

...
On program startup, the integer file descriptors associated with the streams stdin, stdout, and stderr are 0, 1, and 2, respectively. The preprocessor symbols STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO are defined with these values in <>. (Applying (3) to one of these streams can change the file descriptor number associated with the stream.)
...

stdin / stdout / stderr 
分别是指向stream的FILE型的指针变量。
当程序启动时,与其结合的整型文件描述符(fd)分别是0,1,2。

STDIN_FILENO / STDOUT_FILENO / STDERR_FILENO 
是在<>文件中定义的预编译macro。
值是0,1,2。

(通过freopen(3),可以改变与这3个文件描述符(fd)结合的stream值)

也就是说,程序启动时:
FILE * stdin / stdout / stderr 对应的 文件描述符(fd)分别是 STDIN_FILENO(0) / STDOUT_FILENO(1) / STDERR_FILENO(2) 。
但是,可以通过FILE *freopen(const char *path, const char *mode, FILE *stream); 来改变,使文件描述符(fd)对应到其他的stream上。

STDIN_FILENO / STDOUT_FILENO /  STDERR_FILENO这三个文件描述符(fd),可通过freopen(),来改变与之结合的stream。(结合到那个文件stream上,那个文件stream就是变成 标准输入输出)

标准输入stdin
标准输出stdout
err输出stderr         分别改变。



例程序

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h
>


#define CHANGE_BY_FREOPEN /* 需要注释掉,进行切换 */

int main(int argc, char **argv){
  char buf[] = "hello,world\n";

#ifdef CHANGE_BY_FREOPEN
  freopen("stdout_text.txt","w",stdout);

  //freopen("stderr_text.txt","w",stderr);/* stderr */
#endif

  printf("%s",buf);
  fwrite(buf, strlen(buf), 1, stdout);
  write(STDOUT_FILENO, &buf, strlen(buf));

  perror("error out");/* stderr */


  return (0);
}/* 
stdouttest.c */


编译命令,生成可执行文件 stdouttest
$ gcc -o stdouttest stdouttest.c

当 #define CHANGE_BY_FREOPEN 被注释掉,无效的时候:
终端shell窗口中的输出结果是:
$ ./stdouttest
hello,world
hello,world
hello,world
error out: No error
$

注:
3个(hello,world + 换行符)。
1个error的输出
标准输入输出,都还是shell窗口。

当 #define CHANGE_BY_FREOPEN ,有效的时候:
终端shell窗口中的输出结果是:
$ ./stdouttest
error out: No error
$
注:shell窗口中,仅有stderr的输出。

但是,会创建一个名为stdout_text.txt的文件,
该文件中的内容是:
hello,world
hello,world
hello,world

注:
3个(hello,world + 换行符)。
仅标准输出,从shell窗口,改变为stdout_text.txt的文件了。


//freopen("stderr_text.txt","w",stderr);/* stderr */
如果有效的话:
①shell中,什么也不输出。
stdout_text.txt 中,输出3个(hello,world + 换行符)。
stderr_text.txt 中,输出1个(error out: No error)。


上一篇:ccache 编译缓存
下一篇:patch 的制作方法