Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. PLEASE help (me) with syntax for placings redirection to file.

PLEASE help (me) with syntax for placings redirection to file.

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomdebuggingquestionlearning
15 Posts 4 Posters 57 Views 1 Watching
  • 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.
  • L Lost User

    Here is my test code :

     system( "echo q | sudo -S ps -A "); //> resultFile");
                                return;
    

    and partial output to console

    sudo] password for nov25-1: PID TTY TIME CMD
    1 ? 00:00:07 systemd
    2 ? 00:00:00 kthreadd
    3 ? 00:00:00 rcu_gp
    4 ? 00:00:00 rcu_par_gp
    5 ? 00:00:00 slub_flushwq
    6 ? 00:00:00 netns
    8 ? 00:00:00 kworker/0:0H-events_highpri
    11 ? 00:00:00 mm_percpu_wq
    12 ? 00:00:00 rcu_tasks_kthread
    13 ? 00:00:00 rcu_tasks_rude_kthread

    no output with "resultFile" hence same outcome as with original code PS ONLY , so far , the "hcitool info" require password AND current system call passing it works as expected.

    Mircea NeacsuM Offline
    Mircea NeacsuM Offline
    Mircea Neacsu
    wrote on last edited by
    #4

    This works for me:

    #include
    #include

    int main ()
    {
    int ret;
    ret = system ("echo xxxxxx | sudo -S ps -A >results");
    printf ("ret=%d\n", ret);
    return 0;
    }

    The output goes to "results" file and ret is 0. Environment: Ubuntu 22.04LTS freshly installed yesterday. Compiler: gcc 11.1.0

    Mircea

    L 1 Reply Last reply
    0
    • Mircea NeacsuM Mircea Neacsu

      This works for me:

      #include
      #include

      int main ()
      {
      int ret;
      ret = system ("echo xxxxxx | sudo -S ps -A >results");
      printf ("ret=%d\n", ret);
      return 0;
      }

      The output goes to "results" file and ret is 0. Environment: Ubuntu 22.04LTS freshly installed yesterday. Compiler: gcc 11.1.0

      Mircea

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #5

      Just to make sure - how is your results file declared /defined? I am getting correct return value but no data anywhere - no console or file.

      Mircea NeacsuM 1 Reply Last reply
      0
      • L Lost User

        Just to make sure - how is your results file declared /defined? I am getting correct return value but no data anywhere - no console or file.

        Mircea NeacsuM Offline
        Mircea NeacsuM Offline
        Mircea Neacsu
        wrote on last edited by
        #6

        It doesn’t exist before running the program. It is created by the command. If you want to read it, you should open it after the command has finished.

        Mircea

        L 1 Reply Last reply
        0
        • L Lost User

          This is my current resource: https://www.ibm.com/docs/en/zos/2.1.0?topic=line-using-redirection-symbols unfortunately I am not sure WHERE the redirection belongs in my system call parameters. Here is my TEST code

          QFile \*resultFile;
          
                              system(" echo q  | sudo -S hcitool cc --role=c 98:D3:31:F8:29:33 > resultFile");
                              system(" echo q  | sudo -S hcitool info 98:D3:31:F8:39:33  > resultFile");
                              //system(" echo q  | sudo -S hcitool info 98:D3:31:F8:39:33"  > resultFile;
                              return;
          

          and the TEST debug result

          [sudo] password for nov25-1: Can't create connection: Input/output error
          [sudo] password for nov25-1:

          neither one provides data output to "resultFile".
          Please note that the first "system" call attempts to "connect" and fails ,hence
          would not have any data to redirect anyway. Expected with "no
          connection " behavior.( Used to test compiler /linker. )

          I am asking for help placing the ">resultFile" into system call.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #7

          The error message would appear to be from hcitool; see Can't create connection: Input/output error - Google Search[^]. All of which suggests that your code is working, but you misunderstand the output.

          1 Reply Last reply
          0
          • Mircea NeacsuM Mircea Neacsu

            It doesn’t exist before running the program. It is created by the command. If you want to read it, you should open it after the command has finished.

            Mircea

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #8

            Kindly allow me to "write review". The OP question was answered. by placing the redirection as last option of the system call. Ir was verified by passing by complier / linker and function returning success ( 0 ). However, as coded there is no "passed to " function seen in debugger, ( since it is part of the options ?) and there is no practical value of the "passed to " function if it cannot be read. hence there is an extension to my OP how to read the contents of the "passed to" function ? after that question extension is answered there will be another modification how to "pipe" ca;; result to the console and redirect it to a file SAME time ? I sincerely appreciate your contribution to resolve this.

            L Mircea NeacsuM 2 Replies Last reply
            0
            • L Lost User

              Kindly allow me to "write review". The OP question was answered. by placing the redirection as last option of the system call. Ir was verified by passing by complier / linker and function returning success ( 0 ). However, as coded there is no "passed to " function seen in debugger, ( since it is part of the options ?) and there is no practical value of the "passed to " function if it cannot be read. hence there is an extension to my OP how to read the contents of the "passed to" function ? after that question extension is answered there will be another modification how to "pipe" ca;; result to the console and redirect it to a file SAME time ? I sincerely appreciate your contribution to resolve this.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #9

              See Solution 3 at How do I redirect "system" call output ?[^] which shows how to capture the output of a command passed to the shell.

              1 Reply Last reply
              0
              • L Lost User

                Kindly allow me to "write review". The OP question was answered. by placing the redirection as last option of the system call. Ir was verified by passing by complier / linker and function returning success ( 0 ). However, as coded there is no "passed to " function seen in debugger, ( since it is part of the options ?) and there is no practical value of the "passed to " function if it cannot be read. hence there is an extension to my OP how to read the contents of the "passed to" function ? after that question extension is answered there will be another modification how to "pipe" ca;; result to the console and redirect it to a file SAME time ? I sincerely appreciate your contribution to resolve this.

                Mircea NeacsuM Offline
                Mircea NeacsuM Offline
                Mircea Neacsu
                wrote on last edited by
                #10

                I'm not sure I understand your question. Something might be "lost in translation". You cannot send results to file and console "at the same time". What you can do is send them to the console right after you sent them to the file. For instance:

                ps -A >results && cat results

                If what you want is to read the results in your program and process them somehow, you could do something like:

                #include
                #include

                int main ()
                {
                int ret;
                char line[256];
                ret = system ("echo xxxxxx | sudo -S ps -A >results");
                FILE *f = fopen("results", "r");
                while (!feof(f)) {
                fgets (line, sizeof(line), f);
                puts(line);
                }
                fclose(f);
                return 0;
                }

                I didn't compile this code but should be rather OK. If you explain better what you want to do maybe I can answer more to the point.

                Mircea

                K J 2 Replies Last reply
                0
                • Mircea NeacsuM Mircea Neacsu

                  I'm not sure I understand your question. Something might be "lost in translation". You cannot send results to file and console "at the same time". What you can do is send them to the console right after you sent them to the file. For instance:

                  ps -A >results && cat results

                  If what you want is to read the results in your program and process them somehow, you could do something like:

                  #include
                  #include

                  int main ()
                  {
                  int ret;
                  char line[256];
                  ret = system ("echo xxxxxx | sudo -S ps -A >results");
                  FILE *f = fopen("results", "r");
                  while (!feof(f)) {
                  fgets (line, sizeof(line), f);
                  puts(line);
                  }
                  fclose(f);
                  return 0;
                  }

                  I didn't compile this code but should be rather OK. If you explain better what you want to do maybe I can answer more to the point.

                  Mircea

                  K Offline
                  K Offline
                  k5054
                  wrote on last edited by
                  #11

                  Mircea Neacsu wrote:

                  You cannot send results to file and console "at the same time"

                  At the risk of further confusing the OP, I'd like to remind you of the tee command

                  $ ls | tee /tmp/foo
                  PID TTY TIME CMD
                  3995778 pts/1 00:00:00 bash
                  3995865 pts/1 00:00:00 ps
                  3995866 pts/1 00:00:00 tee
                  $ cat /tmp/foo
                  PID TTY TIME CMD
                  3995778 pts/1 00:00:00 bash
                  3995865 pts/1 00:00:00 ps
                  3995866 pts/1 00:00:00 tee

                  But to the OP's issue, I'd like to, once again, point out that system() is not an ideal tool to use if you want to capture command output. A better option would be to use popen() or a QProcess object. The masochists could also write their own process object that allows reading and writing to the child process, but that's an adventure not for the faint of heart.

                  "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

                  Mircea NeacsuM L 2 Replies Last reply
                  0
                  • K k5054

                    Mircea Neacsu wrote:

                    You cannot send results to file and console "at the same time"

                    At the risk of further confusing the OP, I'd like to remind you of the tee command

                    $ ls | tee /tmp/foo
                    PID TTY TIME CMD
                    3995778 pts/1 00:00:00 bash
                    3995865 pts/1 00:00:00 ps
                    3995866 pts/1 00:00:00 tee
                    $ cat /tmp/foo
                    PID TTY TIME CMD
                    3995778 pts/1 00:00:00 bash
                    3995865 pts/1 00:00:00 ps
                    3995866 pts/1 00:00:00 tee

                    But to the OP's issue, I'd like to, once again, point out that system() is not an ideal tool to use if you want to capture command output. A better option would be to use popen() or a QProcess object. The masochists could also write their own process object that allows reading and writing to the child process, but that's an adventure not for the faint of heart.

                    "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

                    Mircea NeacsuM Offline
                    Mircea NeacsuM Offline
                    Mircea Neacsu
                    wrote on last edited by
                    #12

                    k5054 wrote:

                    I'd like to remind you of the tee command

                    True! I forgot about good old tee :) Too much time spent in Windowsland

                    Mircea

                    1 Reply Last reply
                    0
                    • K k5054

                      Mircea Neacsu wrote:

                      You cannot send results to file and console "at the same time"

                      At the risk of further confusing the OP, I'd like to remind you of the tee command

                      $ ls | tee /tmp/foo
                      PID TTY TIME CMD
                      3995778 pts/1 00:00:00 bash
                      3995865 pts/1 00:00:00 ps
                      3995866 pts/1 00:00:00 tee
                      $ cat /tmp/foo
                      PID TTY TIME CMD
                      3995778 pts/1 00:00:00 bash
                      3995865 pts/1 00:00:00 ps
                      3995866 pts/1 00:00:00 tee

                      But to the OP's issue, I'd like to, once again, point out that system() is not an ideal tool to use if you want to capture command output. A better option would be to use popen() or a QProcess object. The masochists could also write their own process object that allows reading and writing to the child process, but that's an adventure not for the faint of heart.

                      "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #13

                      I believe it is time to say that the problem has been solved. I now have enough info to figure out why hcitool info 98:D3:31:F8:39:33 takes almost 20 seconds to "complete", however , it goes from immediate " connect " to "disconnect" in few seconds... (hcitool problem outside CodeProject scope ) In retrospect I would like to point out that I am still not sure how echo q | sudo -S hcitool cc --role=c 98:D3:31:F8:29:33 works how do I get "user" in console , but not "password"? There was two reasons why I shy away from QProcess I am being chastised every time I ask Qt question, but it is OK to keep showing QProcess to me especialy when I do not know what is "program" in echo q | sudo -S hcitool cc --role=c 98:D3:31:F8:29:33 command. (obviously Qt question )

                      L 1 Reply Last reply
                      0
                      • Mircea NeacsuM Mircea Neacsu

                        I'm not sure I understand your question. Something might be "lost in translation". You cannot send results to file and console "at the same time". What you can do is send them to the console right after you sent them to the file. For instance:

                        ps -A >results && cat results

                        If what you want is to read the results in your program and process them somehow, you could do something like:

                        #include
                        #include

                        int main ()
                        {
                        int ret;
                        char line[256];
                        ret = system ("echo xxxxxx | sudo -S ps -A >results");
                        FILE *f = fopen("results", "r");
                        while (!feof(f)) {
                        fgets (line, sizeof(line), f);
                        puts(line);
                        }
                        fclose(f);
                        return 0;
                        }

                        I didn't compile this code but should be rather OK. If you explain better what you want to do maybe I can answer more to the point.

                        Mircea

                        J Offline
                        J Offline
                        jschell
                        wrote on last edited by
                        #14

                        Mircea Neacsu wrote:

                        You cannot send results to file and console "at the same time".

                        In linux/unix you can use 'tee'.

                        1 Reply Last reply
                        0
                        • L Lost User

                          I believe it is time to say that the problem has been solved. I now have enough info to figure out why hcitool info 98:D3:31:F8:39:33 takes almost 20 seconds to "complete", however , it goes from immediate " connect " to "disconnect" in few seconds... (hcitool problem outside CodeProject scope ) In retrospect I would like to point out that I am still not sure how echo q | sudo -S hcitool cc --role=c 98:D3:31:F8:29:33 works how do I get "user" in console , but not "password"? There was two reasons why I shy away from QProcess I am being chastised every time I ask Qt question, but it is OK to keep showing QProcess to me especialy when I do not know what is "program" in echo q | sudo -S hcitool cc --role=c 98:D3:31:F8:29:33 command. (obviously Qt question )

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #15

                          Salvatore Terress wrote:

                          obviously Qt question

                          No, nothing to do with Qt. But then that is (the main) part of your problem, you seem to be confused between what is Qt, the language library or the Linux shell.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups