Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Save Terminal output in a vector of strings
QtWS25 Last Chance

[SOLVED] Save Terminal output in a vector of strings

Scheduled Pinned Locked Moved General and Desktop
c++terminal
6 Posts 3 Posters 2.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nick96
    wrote on 28 Jul 2015, 17:06 last edited by Nick96
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 28 Jul 2015, 17:21 last edited by
      #2

      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);
       
      }
      
      1 Reply Last reply
      0
      • S Offline
        S Offline
        SysTech
        wrote on 28 Jul 2015, 17:47 last edited by SysTech
        #3

        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.

        1 Reply Last reply
        1
        • N Offline
          N Offline
          Nick96
          wrote on 28 Jul 2015, 18:13 last edited by
          #4

          @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 ?

          1 Reply Last reply
          0
          • N Offline
            N Offline
            Nick96
            wrote on 29 Jul 2015, 09:38 last edited by
            #5

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

            M 1 Reply Last reply 29 Jul 2015, 09:39
            0
            • N Nick96
              29 Jul 2015, 09:38

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

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 29 Jul 2015, 09:39 last edited by
              #6

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

              1 Reply Last reply
              0

              1/6

              28 Jul 2015, 17:06

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved