k*f
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;
从来没有用过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
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;
我假设他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;
c*x
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.
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.
相关阅读
c的问题Hadoop写入的主流框架有哪些?有熟悉CUDA的吗?不胜感谢赐教好像核心用C++的公司更成功每次看到g家的东西,我就菊花一紧angular2用jQuery非常方便STL怎样同时重载()和< ?VisualStudio的LoadTest咋入门?搞个pokemon go中国版程序员就是产品/项目经理的爹啊江湖险恶啊90后贪玩CEO,被扎克伯格相中,创20亿收购神话(图)LinkedIn 求team match"学习"就两种有大牛可以解释下bloom filter是在什么条件下使用最好search in dictionary is O(1)关于Capital One的online test先进生产力来了关于 cin >> 操作OAuth 是不是个大烂玩意而