Read the stdout from the same C program

只谈情不闲聊 提交于 2019-12-13 00:14:25

问题


How can I read from the stdout of my program?

One of my threads needs to access the stdout to read what the other threads are logging.

I'm using the dear old plain C.


回答1:


Make stdout use a pipe by replacing the stdout fd with the write side fd of a pipe using dup.




回答2:


You shouldn't. stdout is supposed to be the output to your program and you're not therefore supposed to be reading it in as well. If you want to do this, do your logging elsewhere.




回答3:


If I understand you correctly, you want your threads to communicate with each other. Using stdout for inter-thread communication doesn't seem to be the right way to solve this problem.

Perhaps you should try a Web search for "communication between threads".



来源:https://stackoverflow.com/questions/2228219/read-the-stdout-from-the-same-c-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!