How to reroute a system call output to local variable in C++?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Weegee
    New Member
    • Mar 2008
    • 13

    #1

    How to reroute a system call output to local variable in C++?

    Hello,

    For example, I can do this call to get the output to a text file:

    system("dir > temp.txt");

    But does anyone know how to get the output to a local variable like CString or char*?

    Thanks in advanced.
    -weeg
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    You mean other than opening temp.txt and reading its contents using C++?

    Comment

    • Weegee
      New Member
      • Mar 2008
      • 13

      #3
      Yep. I'd like to skip the i/o step, and process the string directly...that is if it's possible.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Just use printf or cout on your CString and redirect yout program output to a text file.

        CString means you are probably using Visual Studio.NET and that means you go to your project properties for Debugging and put in your command line arguments like >testout.txt. You don't use the program name as a command line argument.

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          Look at the idea of pipes. And piping streams. A UNIX function that's a good search term for the entire topic is popen. Try those, and read about it. Windows can execute a process, and pipe output of the spawned process to your program. Look around, and you'll find out how.

          Comment

          • RRick
            Recognized Expert Contributor
            • Feb 2007
            • 463

            #6
            The problem you have is with the system command. It will run a command for you, but the only return value from system is the return code from the command string. System redirects all output to STDOUT, and I don't know of any way to easily catch that info.

            Also, I don't know any other C/C++ command you can use. Other languages (like bash and perl) have mechanisms to collect output, but C/C++ seems to be missing these mechanisms.

            Comment

            • arnaudk
              Contributor
              • Sep 2007
              • 425

              #7
              If all you want to do is get a directory listing, you might check out boost::filesyst em, or have a look through windows file management on msdn.

              Comment

              • Weegee
                New Member
                • Mar 2008
                • 13

                #8
                Thanks everyone for giving me some suggestions! :) I went ahead and searched around and found CreateProcess() . It's a little involved, but I finally got it working for what I needed to do.

                Thanks again!
                -weeg

                Comment

                Working...