设为首页 加入收藏

TOP

C++ 调用Linux系统命令的简单实例
2018-01-30 12:41:59 】 浏览:169
Tags:调用 Linux 系统 命令 简单 实例

一个简单的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;  
}  
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++ opencv把蓝底照片转化为白底.. 下一篇C++ 计某年某月的天数【多分支】..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目