美文网首页
linux popen与pclose的demo

linux popen与pclose的demo

作者: 十月石榴2013 | 来源:发表于2018-06-18 16:02 被阅读11次

    采用popen打开执行一个shell指令。

    // proc1.cpp
    #include <string>
    #include <iostream>
    #include <stdio.h>
    using namespace std;
    int main()
    {
        string str = "hello";
        cout<<str<<endl;
        FILE* fp = popen("date","w");
        pclose(fp);
        return 11;
    }
    
    结果: image.png

    在执行了proc1的指令之后,还执行了date指令。

    除此之外。linux中“|”也是管道。
    例子:

    // proc2.cpp
    #include <iostream>
    #include <string>
    using namespace std;
    int main(int argc, char **argv)
    {
        string s("hi");
        cin>>s;
        cout<<s<<endl;
        cout<<argv[1]<<endl;
    }
    
    执行proc1|proc2后,结果: image.png

    即proc1的第一个输出(cout<<str<<endl;)。

    相关文章

      网友评论

          本文标题:linux popen与pclose的demo

          本文链接:https://www.haomeiwen.com/subject/xryneftx.html