English 中文(简体)
B. 阅读终端对仪表仪的反应
原标题:Reading terminal response to bash commands into c++ variable
  • 时间:2011-10-13 10:52:13
  •  标签:
  • c++
  • bash

是否有任何人知道从巴什指挥部到C++的终端产出中读懂的方法? 例如,将“ls”输入目前目录中档案名称的终点站,是否可将这些名字输入地表或阵列或某种东西? 我一直在研究<代码>fork(),exec(,>,>. 我试图在儿童(这里的系统指挥)和父母(这里宣读的投稿)之间开一条管道,但完全没有成功。 想法?

最佳回答

Thanks Kerrek SB, I had seen people using popen() in various ways but wasn t sure if that was the route to pursue or not. With the help of the documentation and putting together parts of how other people were using it, I came up with this. This does what the question I wrote asked about specifically: importing the terminal response from the command "ls" into a vector of file names. However, each file name contains a newline character that I implicitly remove pushing back all but the last element of the string file into the vector.

void currentDirFiles( vector<string> & filesAddress )
{ 
    FILE * pipe = popen( "ls", "r" );
    char buffer[1000];
    string file;
    vector<string> files;
    while ( fgets( buffer, sizeof( buffer ), pipe ) != NULL )
    {
        file = buffer;
        files.push_back( file.substr( 0, file.size() - 1 ) );
    }
    pclose( pipe );
    filesAddress.swap( files );
}

我正试图根据曼卡塞的建议,调整“阿里亚加二号”的阴道,以满足我的需求,这是我迄今为止所做的。 它是否像我这样看待? 我对此有明显错误或误解?

 #include <cstdio>

// exceptions
class file_error { } ;
class open_error : public file_error { } ;
class close_error : public file_error { } ;
class read_error : public file_error { } ;

class TerminalPipe
{
   public:
       TerminalPipe():
           pipeHandle(std::popen("ls", "r"))
           {
               if( pipeHandle == NULL )
                   throw open_error() ;
           } 
    ~TerminalPipe()
    {
        std::pclose(pipeHandle) ;
    } 
    void currentDirFiles( vector<string> & filesAddress )
    {
       char buffer[1000];
       string file;
       vector<string> files;
       while ( fgets( buffer, sizeof( buffer ), pipeHandle ) != NULL )
       {
           if( std::ferror( pipeHandle ) ) 
               throw read_error();
           else
           {
               file = buffer;
               files.push_back( file.substr( 0, file.size() - 1 ) );
           }
       }
    }
    private:
        std::FILE* pipeHandle ;
        // copy and assignment not implemented; prevent their use by
        // declaring private.
        TerminalPipe( const file & ) ;
        TerminalPipe & operator=( const file & ) ;
};

Then call it with this:

vector<string> dirFiles;
TerminalPipe pipe;    
pipe.currentDirFiles( dirFiles );
问题回答

如果使用具有约束力的glibmm c++,就可以执行以下规定:

#include <glibmm.h>
#include <iostream>

#include <unistd.h>

void finished_process(int pid, int ret, Glib::RefPtr<Glib::MainLoop>& loop)
{
    loop->quit();
    std::cout << "Process finished: " << pid << "|" << ret <<
    std::endl;
}

int main()
{
    int stdout_fd, stdin_fd, stderr_fd, pid;
    Glib::ustring line;
    std::vector<std::string> command;

    Glib::init();
    command.push_back("ls");
    command.push_back("-l");
    command.push_back("|");
    command.push_back("grep");
    command.push_back("-i");
    command.push_back("my_file");

    try{
    Glib::spawn_async_with_pipes(".", command,
        Glib::SPAWN_SEARCH_PATH | Glib::SPAWN_DO_NOT_REAP_CHILD, sigc::slot<void>(),
        &pid, &stdin_fd, &stdout_fd, &stderr_fd);
    } catch(Glib::SpawnError &e){
    std::cout << "Error: " << e.what() << std::endl;
    return 0;
    }

    Glib::RefPtr<Glib::MainLoop> loop(Glib::MainLoop::create());
    Glib::RefPtr<Glib::MainContext> context = loop->get_context();
    context->signal_child_watch().connect(sigc::bind(sigc::ptr_fun(finished_process), loop),
                    pid);

    Glib::RefPtr<Glib::IOChannel> io = Glib::IOChannel::create_from_fd(stdout_fd);
    loop->run();

    while (io->read_line(line) != Glib::IO_STATUS_EOF){
    std::cout << line;
    }
    io->close();
    close(stdout_fd);
    std::cout << "Exit" << std::endl;
}

To compile the program g++ -g -std=c++0x signal_child_watch_example.cc -o signal_child_watch_example `pkg-config --libs --cflags giomm-2.4`

On Ubuntu/Debian one can install glibmm libraries as follows sudo apt-get install libglibmm-2.4-dev

<><>>>>> 今后,这2.4版的编号可能会改变。 相应修改上述指挥。

来源:rel=“nofollow” http://gtk.10911.n7.nabble.com/Glib-MainContext-signal-child-watch-is-not-working-td43517.html





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?