由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请问如何恢复正常的IO?
相关主题
大家帮我看看C文件输入函数fprintf的问题lex/yacc如何reset buffer?
Python里边file writer的问题弱问perl写网页buffer问题
C中的精度问题Linux Shell 里面象 top 命令的那种自动刷新效果怎么做 ?
请高人解释一下为啥这个输出总是"HELLO-ERR"A question about singleton
printf("%s\n", NULL)的结果Help -- How to output error messages to a file for scripts called by system calls???
c的问题New C++ programmer, need to ask a I/O file read question
A question related to pipe怎样读一个不断更新的文件
发现自己写buffer还是能加速fwrite的gcc 编译的时候要包括 header source file 吗?
相关话题的讨论汇总
话题: stdout话题: dup2话题: fdout话题: fileno话题: fildes2
进入Programming版参与讨论
1 (共1页)
B********s
发帖数: 3610
1
我用dup2把程序的stdout指向了一个文件,那么我该如何使程序的stdout重新指回显示
器? thanks.
k****f
发帖数: 3794
2
把原来的stdout保存下来
最后给恢复一下。

【在 B********s 的大作中提到】
: 我用dup2把程序的stdout指向了一个文件,那么我该如何使程序的stdout重新指回显示
: 器? thanks.

B********s
发帖数: 3610
3
帮忙看看这段代码为什么不能把输出重定向到屏幕?
int main()
{
int fdout;
fdout = open("a",O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
dup2(fdout,STDOUT_FILENO);
printf("hehe\n");
dup2(1,STDOUT_FILENO);
printf("haha\n");
return 0;
}

【在 k****f 的大作中提到】
: 把原来的stdout保存下来
: 最后给恢复一下。

k****f
发帖数: 3794
4
换stdout的时候,需要fflush的。把之前的东西刷出去。
从来没有用过open/dup2
我就是用:fopen造个FILE*
FILE*old_stdout=stdout
stdout=那个FILE*
blahblah....
fflush(stdout);
stdout=old_stdout;

【在 B********s 的大作中提到】
: 帮忙看看这段代码为什么不能把输出重定向到屏幕?
: int main()
: {
: int fdout;
: fdout = open("a",O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
: dup2(fdout,STDOUT_FILENO);
: printf("hehe\n");
: dup2(1,STDOUT_FILENO);
: printf("haha\n");
: return 0;

t****t
发帖数: 6806
5
他这段问题不在这里(因为stdout缺省是line buffered)
我假设他STDOUT_FILENO=1
那么他作的是
dup2(fdout, 1)
...
dup2(1,1)
...
这就好象写:
a1=a_fdout;
// use a1 as a_fdout
a1=a1;
// trying to recover previous a1
这显然不make sense...

【在 k****f 的大作中提到】
: 换stdout的时候,需要fflush的。把之前的东西刷出去。
: 从来没有用过open/dup2
: 我就是用:fopen造个FILE*
: FILE*old_stdout=stdout
: stdout=那个FILE*
: blahblah....
: fflush(stdout);
: stdout=old_stdout;

B********s
发帖数: 3610
6
那么应该怎么写呢?

【在 t****t 的大作中提到】
: 他这段问题不在这里(因为stdout缺省是line buffered)
: 我假设他STDOUT_FILENO=1
: 那么他作的是
: dup2(fdout, 1)
: ...
: dup2(1,1)
: ...
: 这就好象写:
: a1=a_fdout;
: // use a1 as a_fdout

t****t
发帖数: 6806
7
对了,你这个办法不太安全
理论上来说stdout不一定是lvalue,正确的做法是用freopen.当然,freopen就把原来的
给关了.

【在 k****f 的大作中提到】
: 换stdout的时候,需要fflush的。把之前的东西刷出去。
: 从来没有用过open/dup2
: 我就是用:fopen造个FILE*
: FILE*old_stdout=stdout
: stdout=那个FILE*
: blahblah....
: fflush(stdout);
: stdout=old_stdout;

c********x
发帖数: 84
8
Things look more complicated than they appears,
The call:
fid = dup2(fildes, fildes2);
is equivalent to:
close(fildes2);
fid = fcntl(fildes, F_DUPFD, fildes2);
When you "dup2(fdout,STDOUT_FILENO);" you close the stdout, no way
you can use 1 to reopen it.
1 (共1页)
进入Programming版参与讨论
相关主题
gcc 编译的时候要包括 header source file 吗?printf("%s\n", NULL)的结果
求助 怎么编辑 多个 .c files(比如a.c, b.c) 和一个.h file(ab (转载)c的问题
C++ ofstream binary output slowA question related to pipe
does the system guarantee this? (转载)发现自己写buffer还是能加速fwrite的
大家帮我看看C文件输入函数fprintf的问题lex/yacc如何reset buffer?
Python里边file writer的问题弱问perl写网页buffer问题
C中的精度问题Linux Shell 里面象 top 命令的那种自动刷新效果怎么做 ?
请高人解释一下为啥这个输出总是"HELLO-ERR"A question about singleton
相关话题的讨论汇总
话题: stdout话题: dup2话题: fdout话题: fileno话题: fildes2