[SOLVED] Save Terminal output in a vector of strings



  • I just started with Qt creator and after a long search in google I couldn't found the answer I wanted, so I thaught asking here would give me a solution.
    I work in Ubuntu distro.

    Is it possible to run a command in terminal without showing it, but get every line of terminal output saving into a vector of string, in real time, so I can use those data in my program?

    Example:
    Let's say we run the following code which gets the channel frequencys used in my neighborhood, in ascending order. The first number of each line is the number of networks use that channel:

    sudo iwlist wlan0 scan | grep Frequency | sort | uniq -c | sort -n
    

    The output for me is:

      1                     Frequency:2.437 GHz (Channel 6)
      1                     Frequency:2.462 GHz (Channel 11)
      2                     Frequency:2.412 GHz (Channel 1)
    

    I want to save each line of output in different vector element, so in the end I will have 3 vector elements hold the three lines of output. If I had 2 lines then I would have two elements.

    The program is static one time run, but if I want to use some code that generates output in terminal in real time, eg wget, is it possible to update also the info in the vector?


  • Qt Champions 2016

    Hi and welcome
    Here is some code for another user asking to grab the output of "time" cmd.
    Maybe it can be a starting point for you too.

    #include <QStringList>
    #include <QProcess>
    #include <QDebug>
    #include <QByteArray>
     
     
    int main(int argc, char *argv[])
    {
        QProcess exec;
        QStringList arguments;
        arguments <<  "-c" << "time" ;
        exec.start("/bin/sh", arguments);
        int res=exec.waitForStarted();
     
        if (! res )
        qDebug() << exec.errorString();
     
        exec.waitForFinished();
        QByteArray outData1 = exec.readAllStandardOutput();
        qDebug()<<QString(outData1);
        QByteArray outData2 = exec.readAllStandardError();
        qDebug()<<QString(outData2);
     
    }
    


  • I guess the question I have is once you get your lines into the vector what exactly do you want to do with them?

    My reasons for asking are this:

    As mrjj pointed out in his example you can exec your call and gather the input. His example should give you a good start on how to do this.

    Alternatively you could make your program accept data from standard in, put it into the vector and do what you want with it. This way you would not do the exec your program but rather you would pipe it in. Something like:

    sudo iwlist wlan0 scan | grep Frequency | sort | uniq -c | sort -n | myprog
    

    Where myprog is your compiled program.

    The reason for the question is that using mrjj's idea you could have more control over what the program would do perhaps even putting up a menu of choices perhaps.

    In the pipe example I guess you could do the same but generally you want piped programs to just run and be done.



  • @SysTech @mrjj

    When I get the strings, I want to cut again the stings which have the informations into a map (hash-map maybe). In my 'iwlist' command I want to store the numbers of each wireless network who have the same channel as well the channel number itself. In gaphical representation this would be as the follow:

    Num - Channel
    1 6
    1 11
    2 1

    Then that values would be used in another command arguments. That's what I want. How can I achive that ?



  • I found a way to do it with python. Thanksfor the answers!


  • Qt Champions 2016

    @Nick96
    Ok, super. Good luck with the project.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.