在 c/c++ 程序中,可以使用 system()函数运行命令行命令,但是只能得到该命令行的 int 型返回值,并不能获得显示结果。例如system(“ls”)只能得到0或非0,如果要获得ls的执行结果,则要通过管道来完成的。首先用popen打开一个命令行的管道,然后通过fgets获得该管道传输的内容,也就是命令行运行的结果。

在linux上运行的例子如下:

  1. void executeCMD(const char *cmd, char *result)
    {
    char buf_ps[];
    char ps[]={};
    FILE *ptr;
    strcpy(ps, cmd);
    if((ptr=popen(ps, "r"))!=NULL)
    {
    while(fgets(buf_ps, , ptr)!=NULL)
    {
    strcat(result, buf_ps);
    if(strlen(result)>)
    break;
    }
    pclose(ptr);
    ptr = NULL;
    }
    else
    {
    printf("popen %s error\n", ps);
    }
    }

在这段代码中,参数cmd为要执行的命令行,result为命令行运行结果。输入的cmd命令最好用... 2>&1 的形式,这样将标准错误也读进来。

一个完整的例子是:

  1. #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h> int main()
    {
    FILE* fp = NULL;
    char cmd[];
    sprintf(cmd, "pwd 2>/dev/null; echo $?");
    if ((fp = popen(cmd, "r")) != NULL)
    {
    fgets(cmd, sizeof(cmd), fp);
    pclose(fp);
    } //0 成功, 1 失败
    printf("cmd is %s\n", cmd); return ;
    }

有关在 windows 上实现的过程及源码详见:C程序中获得命令行输出结果

C/C++ 程序中调用命令行命令并获取命令行输出结果的更多相关文章

  1. system调用命令行命令而不显示命令行窗口

    system调用命令行命令而不显示命令行窗口 通常用system调用命令行命令时都会弹出黑底白字的命令行窗口,下面的代码可以不显示弹出的命令行窗口. 代码如下 #pragma comment( lin ...

  2. Native Application 开发详解(直接在程序中调用 ntdll.dll 中的 Native API,有内存小、速度快、安全、API丰富等8大优点)

    文章目录:                   1. 引子: 2. Native Application Demo 展示: 3. Native Application 简介: 4. Native Ap ...

  3. 从C#程序中调用非受管DLLs

    从C#程序中调用非受管DLLs 文章概要: 众所周知,.NET已经渐渐成为一种技术时尚,那么C#很自然也成为一种编程时尚.如何利用浩如烟海的Win32 API以及以前所编写的 Win32 代码已经成为 ...

  4. 利用 gnuplot_i 在你的 c 程序中调用 GNUPLOT

    这是一篇非常早曾经写的小文章,最初发表于我的搜狐博客(2008-09-23 22:55).由于自从转移到这里后,sohu 博客就不再维护了,所以把这篇文章也一起挪了过来. GNUPLOT 是一款功能强 ...

  5. Windows命令行命令总结

    转载地址:https://www.cnblogs.com/accumulater/p/7110811.html   1. gpedit.msc-----组策略 2. sndrec32-------录音 ...

  6. MySql命令行命令和SQL语句

    一.常用mysql命令行命令 1.启动MYSQL服务 net start mysql 停止MYSQL服务 net stop mysql 2.netstat -na|findstr 3306 查看被监听 ...

  7. Windows命令行命令集锦

    原文:Windows命令行命令集锦 转自:http://www.me2wg.com/bbs/forum.php?mod=viewthread&tid=15830 winver--------- ...

  8. Windows与Linux的命令行命令对比

    Windows与Linux的命令行命令对比 * Windows不区分大小写,Linux区分大小写的. sn DOS Command UNIX Equivalent Effect 影响 1 ASSIGN ...

  9. C++程序中调用WebService的实现

    前言 因为最近的项目中需要运用到在MFC程序中调用WebService里面集成好了的函数,所以特意花了一天的时间来研究WebService的构建以及如何在MFC的程序中添加Web引用,进而来实现在C+ ...

随机推荐

  1. codevs 1277 生活大爆炸 2012年CCC加拿大高中生信息学奥赛

     时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题目描述 Description Sheldon and Leonard are physicists wh ...

  2. leetcode_1048. Longest String Chain_[DP,动态规划,记忆化搜索]

    1048. Longest String Chain https://leetcode.com/problems/longest-string-chain/ Let's say word1 is a ...

  3. 图像处理框架 Core Image 介绍

    这篇文章会为初学者介绍一下 Core Image,一个 OS X 和 iOS 的图像处理框架. 如果你想跟着本文中的代码学习,你可以在 GitHub 上下载示例工程.示例工程是一个 iOS 应用程序, ...

  4. poj3264 划分树

    题意: 给定一个序列,询问区间中最大数减去最小数的结果 和2104差不多, 代码贴过来就OK了 #include <iostream> #include <algorithm> ...

  5. Codeforces Round #275(Div. 2)-C. Diverse Permutation

    http://codeforces.com/contest/483/problem/C C. Diverse Permutation time limit per test 1 second memo ...

  6. CSS - position属性小结

    Relative: 属于文档流,针对自身进行偏移: Absolute: 脱离文档流,针对最近的定位元素进行偏移,如果没有,则针对根元素,即body标签尽心偏移: Fixed: 和absolute基本一 ...

  7. ios之UISegmentedcontol

    初始化UISegmentedControl NSArray *arr = [[NSArray alloc]initWithObjects:@"轻拍",@"长按" ...

  8. std::ios::sync_with_stdio和tie()——给cin加速

    平时在Leetcode上刷题的时候,总能看到有一些题中最快的代码都有这样一段 static const auto init = []() { std::ios::sync_with_stdio(fal ...

  9. vim中使用pydiction对python代码进行补全

    在配置完vim插件YouCompleteMe之后,在vim编写python的时候按tab键会报错,E121: Undefined variable: g:pydiction_location Pydi ...

  10. 【php】运算符优先级界定

    <?php $i = 1; $array[$i] = $i++; print_r($array);die; //输出 Array([2] => 1) $a = 1; echo $a + $ ...