gpt4 book ai didi

c++ - 如何在 C++/Linux 中执行外部命令?

转载 作者:IT老高 更新时间:2023-10-28 21:49:03 25 4
gpt4 key购买 nike

我只是想知道在 C++ 中执行外部命令的最佳方式是什么,如果有输出,我该如何获取?

编辑:我想我不得不说我是这个世界上的新手,所以我想我需要一个可行的例子。例如我想执行如下命令:

ls -la

我该怎么做?

最佳答案

使用 popen功能。

示例(不完整,生产质量代码,无错误处理):

FILE* file = popen("ls", "r");
// use fscanf to read:
char buffer[100];
fscanf(file, "%100s", buffer);
pclose(file);

关于c++ - 如何在 C++/Linux 中执行外部命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/671461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com