【问题标题】:How to execute bash commands from C? [duplicate]如何从C执行bash命令? [复制]
【发布时间】:2015-06-28 00:32:52
【问题描述】:

有没有办法运行命令行实用程序,例如gzip,进入 C 应用程序?

【问题讨论】:

  • 为什么不直接使用 c 库进行(解)压缩(zlib)?
  • 如果你把它作为一个“bash 命令”运行,一个 la system(),你做错了。使用 exec-family 系统调用直接调用效率更高且不易出错。

标签: bash c gzip


【解决方案1】:

使用system():

#include <stdlib.h>
int status = system("gzip foo");

有关如何使用它的更多详细信息,请参阅手册页 (man 3 system)。

对了,这个问题在这里已经有了答案:How do I execute external program within C code in linux with arguments?

【讨论】:

  • 见:Why to avoid system() function in c/c++。而是查看execl 系列函数。
  • man system # 似乎默认为 3
  • 手册第 1 节中没有系统条目 手册第 2 节中没有系统条目,因此从 3 开始
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 2019-05-04
  • 2012-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-17
相关资源
最近更新 更多