如何从 C 运行外部程序并解析其输出? [英] How can I run an external program from C and parse its output?

查看:22
本文介绍了如何从 C 运行外部程序并解析其输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实用程序可以输出游戏所需的文件列表.如何在 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 移植.请注意,我正在寻找一种编程方式来执行该实用程序并保留其输出(输出到标准输出).

UPDATE: Good call on the lack of information. The utility spits out a series of strings, and this is supposed to be 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-ish 环境中的简单问题,请尝试 popen().

For simple problems in Unix-ish environments try popen().

来自手册页:

popen() 函数通过创建管道、分叉和​​调用 shell 来打开一个进程.

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屋!

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