如何使用fprintf中并写入到一个管道? [英] How can I use fprintf and write to a pipe?

查看:166
本文介绍了如何使用fprintf中并写入到一个管道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个管道,我用dup2()覆盖流1安培; 2(标准输出和放大器;标准错误)到这些管道

I created a pipe and I used dup2() to overwrite streams 1 & 2 (stdout & stderr) into those pipes.

现在我希望用fprintf中写入数据流1或2,但我的计划似乎并没有被接受在管道的另一端什么。我用printf()试过了,但我不知道这是否默认写入stdout或流1。如果写入数据流1,我在code更深的地方猜测它有问题。

Now I wish to use fprintf to write to stream 1 or 2, but my program doesn't seem to be receiving anything on the other side of the pipe. I've tried using printf(), but I'm not sure if this writes to stdout or stream 1 by default. If it writes to stream 1, I guess its a problem somewhere deeper in my code.

基本上我问,给出一个int重新presenting流,我怎样才能得到适合于使用fprintf中()?

Essentially I'm asking, given an int representing the stream, how can I get a FILE* suitable for use in fprintf()?

推荐答案

如果你有一个文件描述符,并希望有一个 FILE * ,您可以用 fdopen

If you have a file descriptor and want a FILE*, you can use fdopen

FILE *fdopen(int fd, const char *mode);

fdopen 是一个POSIX函数,并在男人fdopen 。做你可以使用反向 的fileno

fdopen is a Posix function and documented in man fdopen. To do the reverse you can use fileno

这篇关于如何使用fprintf中并写入到一个管道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆