Thread: console input/outpu

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    console input/outpu

    Hi,

    I search for an example how to interact with the cmd of windows. all commands I sent to my programm should go to the cmd and the response from the cmd back to my programm and so back to the user.

    So when I type in my programm dir c:\ this command should go to the cmd and the response (the dir listing) back to my prog.

    Thx for any ideas?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    One way would be to pipe the output of cmd to a file.

    To build off of your example:
    When you type
    dir c:\
    in a command-prompt you will get a big list of all the directorys right. Now if you type this instead:
    dir c:\ > c:\result.txt
    You wont see anything. The output will be stored in a textfile for you.

    This way you can then in your own program read all that in and do whatever you want. To do this in a program you can do something like this:
    system("dir c:\ > c:\result.txt");

    There are probably better ways but this should get you started.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    system("dir c:\ > c:\result.txt");
    ->
    Code:
    system("dir c:\\ > c:\\result.txt");
    If you don't mind running your program from a command prompt, you can do this (which redirects the output of one program, dir, into the input of another, program):
    Code:
    C:\>dir *.c | program
    Then you just read from the standard input (ie, cin).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Quote Originally Posted by dwks
    Code:
    system("dir c:\ > c:\result.txt");
    ->
    Code:
    system("dir c:\\ > c:\\result.txt");
    If you don't mind running your program from a command prompt, you can do this (which redirects the output of one program, dir, into the input of another, program):
    Code:
    C:\>dir *.c | program
    Then you just read from the standard input (ie, cin).
    For example I have an console application and I use this pipe thing then my programm get startet with the output of the other programm, so I can use it. But my application is a dll , any ideas how to do it in that case? And the application under which my dll runs I can't change so theres no way to get the output to the exe and then to the dll!

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well then look at my first example. Or better yet use opendir()+readdir()+closedir() to achive what you want. http://faq.cprogramming.com/cgi-bin/...&id=1044780608
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console, Terminal and Terminal Emulator
    By lehe in forum C Programming
    Replies: 4
    Last Post: 02-15-2009, 09:59 PM
  2. Full Screen Console
    By St0rmTroop3er in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2005, 09:59 PM
  3. Problems with a simple console game
    By DZeek in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2005, 02:02 PM
  4. Console Functions Help
    By Artist_of_dream in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 03:44 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM