将Popen()输出写入文件
我一直在尝试从 C++ 调用另一个程序,并将该程序的粗壮内容保存到文本文件中。 popen() 似乎是合适的函数,但将其保存到文本文件不起作用。
ofstream delaunayfile;
delaunayfile.open ("triangulation/delaunayedpoints.txt");
FILE *fp;
fp = popen("qdelaunay < triangulation/rawpoints.txt", "r");
delaunayfile << fp;
delaunayfile.close();
有什么帮助吗?提前致谢!
I've been trying to call another program from c++, and save the stout of that program to a text file. popen() seems to be the appropriate function, but saving it to a text file isn't working.
ofstream delaunayfile;
delaunayfile.open ("triangulation/delaunayedpoints.txt");
FILE *fp;
fp = popen("qdelaunay < triangulation/rawpoints.txt", "r");
delaunayfile << fp;
delaunayfile.close();
Any help? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能将
FILE*
直接写入流中。它将写入内存地址而不是实际的文件内容,因此它不会给您所需的结果。理想的解决方案是从
ifstream
读取并写入ofstream
,但无法从构造
。ifstream
文件*但是,我们可以扩展
streambuf
类,使其在FILE*
上工作,然后将其传递给istream
。快速搜索发现有人已经实现了该功能,并正确命名为popen_streambuf
。请参阅这个具体答案。然后您的代码将如下所示:
正如 Simon Richter 在评论中指出的,有一个
operator<< ;
接受streambuf
并将数据写入ostream
直到到达 EOF。这样,代码将被简化为:You cannot write a
FILE*
directly into a stream. It will write a memory address instead of the actual file contents, therefore it will not give you the desired result.The ideal solution would be to read from an
ifstream
and write to yourofstream
, but there's no way to construct anifstream
from aFILE*
.However, we can extend the
streambuf
class, make it work over aFILE*
, and then pass it to anistream
instead. A quick search revealed someone already implemented that, and properly namedpopen_streambuf
. See this specific answer.Your code then would look like this:
As pointed by Simon Richter in comments, there's an
operator<<
that acceptsstreambuf
and writes data toostream
until EOF is reached. This way, the code would be simplified to:有两种方法可以做到这一点:简单的方法
和稍微复杂的方法,使用 fork()、dup2() 和 execve(),后者无需在系统上安装 shell 解释器即可工作。考虑到这看起来像是你正在做计算工作,我怀疑这不是一个嵌入式系统,所以你可以假设一个工作外壳。
There are two ways to do this: The simple way
and the slightly more elaborate way, using fork(), dup2() and execve(), the latter working without a shell interpreter installed on the system. Given that this looks like you are doing computation work, I suspect this is not an embedded system, so you can assume a working shell.
popen 打开一个管道,但我不知道您可以通过这种方式将其流式传输到 delaunayfile 中。
当然,如果您可以这样做并且它会从管道中读取直到完成,那就太好了。
检查管道上数据的正常方法是使用 select()。我找到了一个有用的链接 http://codenewbie.com/ forums/threads/2908-Using-std-fstream-in-a-pipe 将管道与 fstream 集成在一起,它可以帮助您实现您想要的目标。
在这种情况下,虽然您想要做的就是将输出写入文件,但为什么不将进程的输出重定向到文件而不是管道呢?管道的目的是进程间通信,但您的进程似乎并未将其从其他进程接收的数据用于任何实际目的。
popen opens a pipe but I am not aware you can just stream it into delaunayfile that way.
Of course it would be very nice if you could just do that and it would read from the pipe until it was complete.
The normal way to check for data on the pipe is to use select(). I found a useful link http://codenewbie.com/forums/threads/2908-Using-std-fstream-in-a-pipe that integrates pipes with fstream though and it may help you achieve what you want.
In this instance though as all you want to do is write the output to a file, why not redirect the output of the process to it rather than to a pipe? The purpose of a pipe is Inter-Process communication but your process does not appear to be using the data it receives from the other process for any practical purpose.