C++ 调用Linux系统命令

来源:互联网 发布:java字符串方法 编辑:程序博客网 时间:2024/05/30 23:31


一个简单的C++程序,Test函数用来测试调用Linux的系统命令ls -l

#include<cstdlib>#include<string>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int N = 300;void Test(void){    char line[N];    FILE *fp;    string cmd = "ls -l";    // system call    const char *sysCommand = cmd.data();    if ((fp = popen(sysCommand, "r")) == NULL) {        cout << "error" << endl;        return;    }    while (fgets(line, sizeof(line)-1, fp) != NULL){        cout << line << endl;    }    pclose(fp);}int main(){    Test();    return 0;}




0 0
原创粉丝点击