流打印和重定向
我有一个程序,它(通过 printf)打印到 stdout
一些数据,并调用函数 *foo*
它还会向 stdout
打印一些数据 [从 foo 进行打印的方式(实现)未知,我看不到 foo 的代码]。
我必须将所有内容从 stdout
重定向到 buffer
或文件。我尝试通过多种方式来做到这一点
freopen(file.txt, stdout)
- 只有我的代码打印被写入 file.txt。从 foo 打印的内容丢失了。setbuf(buffer, stdout)
- 只有我的代码打印被写入缓冲区。从 foo 打印的内容出现在标准输出中。(它出现在屏幕上)
什么可以解释这种行为?问题如何解决?
注意:此代码必须在跨操作系统(lunux/wind && mac OS)中工作。我使用 gcc 来编译代码,并且我有 cygwin
I have a program which prints (by printf) to the stdout
some data and also calls to function *foo*
which also prints to the stdout
some data [the way (implementation) of how printing is done from foo is unknown and I can`t see the code of foo].
I have to redirect everything from stdout
to buffer
or file. I tried to do it in several ways
freopen(file.txt, stdout)
- only my code prints are written to the file.txt. What was printed from foo is lost.setbuf(buffer, stdout)
- only my code prints are written to the buffer. What was printed from foo is appears in the stdout.(It appears on the screen)
What can explain this behavior? How can the problem be solved?
Note:This code has to work in cross-OS( lunux/wind && mac OS).I use gcc in order compile the code and I have cygwin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很可能
foo
没有使用stdio
进行打印并直接调用操作系统。我不知道 win32,但在 POSIX 上你可以使用
dup2< /code>
来处理它。
编辑
令我惊讶的是,
win32 有但它还有其他作用。_dup2
It's likely that
foo
isn't usingstdio
for printing and directly calling the OS for this.I don't know about win32, but on POSIX you could use
dup2
to take care of it.EDIT
Much to my surprise,
win32 hasbut it does something else._dup2
您如何知道
foo()
正在打印到stdout
?您是否尝试过将标准输出重定向到 shell 中的文件并查看 foo() 的输出是否仍然出现在屏幕上?如果文件重定向将 foo() 的输出发送到文件,那么您可能必须重新调整文件描述符级别,如 cnicutar的回答。
如果文件重定向没有将
foo()
的输出发送到文件,则它可能正在写入stderr
或者可能正在打开并使用/dev /tty
或类似的东西。您可以通过与stdout
分开重定向来测试stderr
:如果它打开
/dev/tty
,输出仍会出现在您的屏幕上。你在哪个平台?如果您可以跟踪系统调用(Linux 上的
strace
,Solaris 上的truss
,...),那么您也许能够看到foo( )
函数正在执行。您可以通过在调用函数之前和之后编写一条消息来提供帮助,并确保刷新输出:printf/fflush 调用将在跟踪输出中可见,因此两者之间出现的内容是由 foo() 完成的代码>.
How do you know that
foo()
is printing tostdout
? Have you tried redirecting standard output to a file at the shell and seeing whether the output fromfoo()
still appears on the screen?If the file redirection sends
foo()
's output to the file, then you may have to rejig the file descriptor level, as in cnicutar's answer.If the file redirection does not send
foo()
's output to the file, then it may be writing tostderr
or it may be opening and using/dev/tty
or something similar. You can test forstderr
by redirecting it separately fromstdout
:If it is opening
/dev/tty
, the output will still appear on your screen.Which platform are you on? If you can track system calls (
strace
on Linux,truss
on Solaris, ...), then you may be able to see in that what thefoo()
function is doing. You can help things by writing a message before and after calling the function, and ensuring you flush the output:The printf/fflush calls will be visible in the trace output, so what appears between is done by
foo()
.当您调用的代码使用与您的代码不同的 C 库时,我见过这种行为。在 Windows 上,当一个 DLL 使用 GCC 编译而另一个使用 Visual C++ 编译时,我经常看到这种情况。这些的
stdio
实现明显不同,因此这可能会出现问题。另一个原因是您调用的代码未使用
stdio
。如果您使用的是 Unix,您可以使用 dup2 来解决这个问题,例如。dup2(my_file_descriptor, 1)
。在许多实现中,如果您有一个FILE*
,您可以说dup2(fileno(f), 1)
。这可能不便于携带。I have seen this sort of behavior when the code you are calling into uses a different C library than yours. On Windows I used to see this sort of thing when one DLL is compiled with GCC and another with Visual C++. The implementation of
stdio
for these is apparently different enough such that this can be problematic.Another is that the code you are calling is not using
stdio
. If you are on Unix you can usedup2
to get around this, eg.dup2(my_file_descriptor, 1)
. On many implementations if you have aFILE*
you can saydup2(fileno(f), 1)
. This may not be portable.