如何从C运行外部程序并解析其输出?我有一个实用工具,它输出游戏所需的文件列表。如何在C程序中运行该实用程序并获取其输出,以便在同一个程序中对其进行操作?更新:很好的呼吁缺乏信息。该实用工具释放出一系列字符串,这应该是在Mac/Windows/Linux上完全可移植的。请注意,我正在寻找一种编程方法来执行该实用程序并保留其输出(这将进入stdout)。
                    
                    
                3 回答
 
                    
                    
                            海绵宝宝撒
                            
                                
                            
                        
                        
                                                
                    TA贡献1809条经验 获得超8个赞
popen()
#include <stdio.h>#define BUFSIZE 128int parse_output(void) {
    char *cmd = "ls -l";    
    char buf[BUFSIZE];
    FILE *fp;
    if ((fp = popen(cmd, "r")) == NULL) {
        printf("Error opening pipe!\n");
        return -1;
    }
    while (fgets(buf, BUFSIZE, fp) != NULL) {
        // Do whatever you want here...
        printf("OUTPUT: %s", buf);
    }
    if(pclose(fp))  {
        printf("Command not found or exited with error status\n");
        return -1;
    }
    return 0;}- 3 回答
- 0 关注
- 632 浏览
添加回答
举报
0/150
	提交
		取消
	
