您的位置:首页 > 运维架构

freopen (stdin stdout ) 详解+实例

2011-11-29 12:01 288 查看
http://www.cnitblog.com/guopingleee/archive/2009/01/17/53843.aspx

找一大圈子.........现在看到了点自己能看懂的东西.
想不通就慢慢思考! 一定能懂的. 仔细看....都会讲出来...

网上这个东西有点难找.

代码:
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-

[26047 0] #[/color] cat freopen.c

#include <stdio.h>

#include <unistd.h>

int

main()

{

FILE *fp;

char buf[1024];

char tty[32];

ttyname_r(fileno(stdin), tty, sizeof(tty));

fp = freopen("./freopen.c", "r", stdin);

fgets(buf, sizeof(buf), stdin);

printf("%s", buf);

fp = freopen(tty, "r", stdin);

fgets(buf, sizeof(buf), stdin);

printf("%s", buf);

return 0;

}

[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-

[26047 0] #[/color] gcc freopen.c

[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-

[26047 0] #[/color] ./a.out

#include <stdio.h>

This line is inputed by me.

This line is inputed by me.

[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-

[26047 0] #[/color] o


--------------------next---------------------

谢谢 dearvoid !$ ~ y+ M( v+ C$ g$ l

; U: x' i! \" j% c" @2 `/ M

只要把"/dev/console"改成"/dev/tty"就可以了" f7 a8 u* z1 e" k& {- \

7 u9 h) N* D" K5 v; l

/dev/console  代表的是系统控制台,每个LINUX系统都会有一个专门的终端或显示屏用来接收控制台消息。通常就是“活跃”的虚拟控制台,而在X Windows系统下,它会是屏幕上一个特殊的控制台窗口。

]2 w, m) A. x) G3 I a! q. t; R

3 t' g. S2 [8 N7 F特殊文件 /dev/tty  是进程控制终端(键盘和显示屏,或者键盘和窗口)的一个别名(逻辑设备)--如果这个程序有控制终端的话。 /dev/tty 允许程序直接向用户输出信息,不管用户具体使用的是哪种类型的伪终端。在标准输出被重定向的情况下,这一功能非常有用。

- n' ]% \6 d. s9 E A1 @( e

& f3 R8 i9 B1 P8 O/dev/console 设备只有一个,但通过 /dev/tty 能够的物理设备却可以说是数不胜数。      

freopen之后如何恢复标准句柄

By skywind

通常可以使用freopen将输入/输出重定向到文件中。例如

[CPP]freopen(”in.txt”, “r”, stdin);

freopen(”out.txt”, “w”, stdin);

[/CPP]

但并不存在一个完全兼容的解决方案能够在以后将标准句柄恢复。在C标准库里面是没有办法的。

很容易想到的方式是重新打开标准控制台设备文件,但遗憾的是,这个设备文件的名字是操作系统相关的。

在DOS/Win中,这个名字是CON,因此可以使用

[cpp]freopen(”CON”, “r”, stdin)[/cpp]

在linux中,控制台设备是 /dev/console

[cpp]freopen(”/dev/console”, “r”, stdin)[/cpp]

另外,在类unix系统中,可以使用dup系统调用来预先复制一份原始的stdin句柄。

使用C++的流库,可以有另外一种方式达到freopen的效果,而且很容易恢复。就是使用流的rdbuf。例如:

[CPP]

ofstream outf(”out.txt”);

streambuf *strm_buf = cout.rdbuf();

cout.rdbuf(outf.rdbuf());

cout << “write something to file”;

cout.rdbuf(strm_buf); //recover

cout << “display something on screen”;

[/CPP]

===========================================================

  函数名: freopen

  功 能: 替换一个流

  用 法: FILE *freopen(char *filename, char *type, FILE *stream);

  程序例:

  #include <stdio.h>

  int main(void)

  {

  /* redirect standard output to a file */

  if (freopen("OUTPUT.FIL", "w", stdout)

  == NULL)

  fprintf(stderr, "error redirectingstdout\n");

  /* this output will go to a file */

  printf("This will go into a file.");

  /* close the standard output stream */

  fclose(stdout);

  return 0;

  }

===========================================================

上面不懂, 可以向下看, 没关系. 实践+理论 , 会慢慢在这详解.., 慢慢看.

下面重点:

在这再说一下. 不然很难理解, 我都没想到. 一直困惑不清啊....stdin stdout stderr.

现在懂了.

牢记: 目前主要的缓存特征是:stdin和stdout是行缓存;而stderr是无缓存的。

本文介绍如何将 stdout 时重定向到文件从某个 C 的程序,然后还原原始的 stdout 同一程序的某个更高位置。 C 函数通常用于重定向 stdout 或 stdin 是 freopen()。 要将 stdout 时重定向到文件名为 FILE.TXT 中,使用下面的调用:

freopen( "file.txt", "w", stdout ); //把内容写到这个文件"file.txt"


此语句使所有的后续输出,通常定向到转到文件 FILE.TXT stdout,向。

若要返回到显示默认 stdout) 的 stdout,使用下面的调用:

freopen( "CON", "w", stdout ); //输出到控制台"CON"


在这两种情况下检查 freopen() 以确保重定向实际发生的返回值。

下面是短程序演示了 stdout 时重定向:

运行代码


// Compile options needed: none

#include <stdio.h>

#include <stdlib.h>void main(void)

{

FILE *stream ;

//将内容写到file.txt,
"W"是写 ("r"是读)

if((stream = freopen("file.txt", "w", stdout)) == NULL)

exit(-1);

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

stream = freopen("CON", "w", stdout);stdout 是向程序的末尾的控制台重定向

printf("And now back to the console once again\n");

}


"CON" 是指控制台 就想DOS窗口.

==========================================

运行代码:

#include <stdio.h>

#include <stdlib.h>

void main(void)

{

FILE *stream ;

char s[102400]="";

if((stream = freopen("file.txt", "r", stdin)) == NULL) //从文件读数据 (放到stdin , 其实stdin 也有自己的缓冲区.就向buf)

exit(-1);

fread(s, 1, 1024, stdin); //所以从标准输入里读出数据. 因为要注意stdin也是有自己的一块缓冲区.

printf("%s\n", s); //在这里打印读出来的数据.

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: