Thread: Using C++ to run a Console APP?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    1

    Using C++ to run a Console APP?

    I'd like to use C++ to run a console app (telnet), but to change the input everytime. I want to do this to speed up the manual input (typing) into the program. Pseudocode for this would be
    Code:
    int TRY=1;
    char YN
    
    do
    {
    Run Program Telnet
    Input "o" into Telnet
    Input 'ENTER' into Telnet
    Input "111.111.111.111"<<TRY  into Telnet
    TRY++
    Ask to do again
    }while (YN='y')
    How would I accomplish this?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You want to look into piping, me thinks. You can call a program from the command line, and specify a file to be used as the standard input from the program. Instead of waiting for input for the keyboard, it will take the next piece of data from the file and use that instead. The same rules apply for getting data. If a function you use to get data from the keyboard would stop at a white space character or a new line, it would stop at the same character in the file.

    You can run the program and pipe input to it using the system command. Like this (been a while since I piped - this is just a general idea - research it for your OS first).

    Code:
    #include <cstdio>
    ...
        system("Telnet | input.txt");
    There's a similar method for specifying a file to be used as output. You can also have one program use another output for input and vice-versa. Very fun stuff.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by sean_mackrory
    You want to look into piping, me thinks. You can call a program from the command line, and specify a file to be used as the standard input from the program. Instead of waiting for input for the keyboard, it will take the next piece of data from the file and use that instead. The same rules apply for getting data. If a function you use to get data from the keyboard would stop at a white space character or a new line, it would stop at the same character in the file.

    You can run the program and pipe input to it using the system command. Like this (been a while since I piped - this is just a general idea - research it for your OS first).

    Code:
    #include <cstdio>
    ...
        system("Telnet | input.txt");
    There's a similar method for specifying a file to be used as output. You can also have one program use another output for input and vice-versa. Very fun stuff.
    Isn't it:
    Code:
    system("Telnet < input.txt");
    that redirects input to come from the file "input.txt"? The other way will not do anything, maybe an error since input.txt is not a program that can be piped to ("'input.txt' is not recognized as an internal or external command, operable program or batch file" is the message I get on my machine WinXP).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    On POSIX, popen would be what you're looking for to dynamically create input and examine output (as you'd probably want in an interactive GUI frontend to a console app). On Windows, I think popen doesn't exist. CreateProcess can redirect the standard streams, though.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by CornedBee
    On Windows, I think popen doesn't exist. CreateProcess can redirect the standard streams, though.
    Although I believe _popen does exist (never used it personally).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console app termination
    By C+/- in forum C++ Programming
    Replies: 2
    Last Post: 05-24-2008, 11:29 AM
  2. Replies: 2
    Last Post: 12-22-2006, 08:45 PM
  3. App to run as service and normal process
    By knutso in forum Windows Programming
    Replies: 4
    Last Post: 12-04-2006, 12:12 PM
  4. Compiled App as release - won't run - as debug runs
    By XenoCodex Admin in forum C++ Programming
    Replies: 7
    Last Post: 08-05-2002, 04:43 PM
  5. Turning a Console APP into Windows.
    By Darkflame in forum C++ Programming
    Replies: 3
    Last Post: 09-14-2001, 03:10 AM