我怎样才能从C运行外部程序,并分析它的输出? [英] How can I run an external program from C and parse its output?
问题描述
我有一个实用程序,输出一个游戏所需的文件列表。我怎样才能运行C程序中的实用程序,并抓住它的输出,所以我可以在同一程序内就此采取行动?
I've got a utility that outputs a list of files required by a game. How can I run that utility within a C program and grab its output so I can act on it within the same program?
更新:对信息的缺乏良好的通话。该实用程序吐出一串字符串,这应该是整个的Mac / Windows / Linux版完全便携。请注意,我正在寻找执行程序并保留其输出(输出到stdout)编程方法。
UPDATE: Good call on the lack of information. The utility spits out a series of strings, and this is supposed to be complete portable across Mac/Windows/Linux. Please note, I'm looking for a programmatic way to execute the utility and retain its output (which goes to stdout).
推荐答案
有关在Unix中上下的环境简单的问题尽量的 的popen()
。
For simple problems in Unix-ish environments try popen()
.
从手册页:
器一起()函数通过创建一个管道,分叉和调用外壳打开一个过程。
The popen() function opens a process by creating a pipe, forking and invoking the shell.
如果您使用的阅读模式,这正是你问什么。我不知道如果是在Windows中执行。
If you use the read mode this is exactly what you asked for. I don't know if it is implemented in Windows.
对于更复杂的问题,你要查找的进程间通信。
For more complicated problems you want to look up inter-process communication.
这篇关于我怎样才能从C运行外部程序,并分析它的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!