当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C++ 调用Linux系统命令的简单实例

C++ 调用Linux系统命令的简单实例

2018年01月26日  | 移动技术网IT编程  | 我要评论

咸阳新闻,mp3 音乐,快乐女生nanana

一个简单的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;  
}  

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网