#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    12

    can't record output sent to a dos window to a file


    I'm putting together a little utility to make my job easier. I use "ping" and "tracert" and I need to grab the ip of the machine i'm on a lot so instead of typing out 'ipconfig' or 'ipconfig /all', this thing will let me just hit '1' or whatever menu item i've setup for that.

    Here's my question. I'd like to take the output and stick it in a file. I'm pretty new to c++ but I have used files before and entered data into them. In this case, i'm a little stuck on how to do it. Here's my code..
    PHP Code:
    cout << "Enter IP:\n";
    cin >> address;
    cout << "Pinging " << address << "...\n" ;
    mycommand "ping " address;
    system(mycommand.data());

    rec_data system(mycommand.data());
    ofstream outFile
    outFile.open("ping.dat",ios::app);
    outFile << rec_data << endl;
    outFile.close();
    system("PAUSE"); 
    Now, I know it's wrong because, well, it doesn't work. heh. All it does is run the same ping twice. Instead, i'd like it to take the output and format it in a text (or dat as I have in the code) file exactly how it's shown in the dos window. Like this...

    PHP Code:
    Pinging xx.xx.xxx.xx with 32 bytes of data:

    Reply from xx.xx.xxx.xxbytes=32 time<1ms TTL=128
    Reply from xx
    .xx.xxx.xxbytes=32 time<1ms TTL=128
    Reply from xx
    .xx.xxx.xxbytes=32 time<1ms TTL=128
    Reply from xx
    .xx.xxx.xxbytes=32 time<1ms TTL=128

    Ping statistics 
    for xx.xx.xxx.xx:
        
    PacketsSent 4Received 4Lost (0loss),
    Approximate round trip times in milli-seconds:
        
    Minimum 0msMaximum 0msAverage 0ms 
    Thanks in advance for any help.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    There are a few ways to handle this:
    1. Use popen() instead of system(). If you search this forum for "popen", you'll find several examples of this code. Here's one recent thread:
    call perl program within C

    2. If you have the Socket++ class library installed, you can use an ipipestream object from the library.
    http://www.lousy.org/man/socket++/socket++_3.html
    http://www.lousy.org/man/socket++/so..._11.html#SEC36

    Hope this helps
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    Try adding "> filename" to the command you send to system().
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    12
    Wow, fast replies. Gotta love that.

    Ok, i'm really really new to this so, i'm not getting exactly how to use the popen() instead of system. How would I quickly change my code to use the popen() option?

    I'd like to be able to change my code as little as possible from it's current state so, if I can just add "> filename" into my existing code, where, or how wuold I add it?

    Is one better than the other?

    Thanks again
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    Code not really tested, but it should look something like this. For microsoft VC++, use _popen() and _pclose() instead of popen() and pclose()
    Code:
    
    #include <cstdio> 
    #define BUFSIZE 4096
    using namespace std;
    
    ...
    ...
    cout << "Enter IP:\n";
    cin >> address;
    cout << "Pinging " << address << "...\n" ;
    mycommand = "ping " + address;
    
    
    char buf[BUFSIZE + 1];
    FILE *pipe;
    pipe = popen(mycommand.c_str(),"r");
    
    if (!pipe) {
         fprintf (stderr, "Error. Could not open pipe\n");
         return 0;
    }
    
    while (fgets(buf, 512, stream)){
        cout << buf << endl;
    }
    
    pclose (stream);
    
    system("PAUSE");
    See http://msdn.microsoft.com/library/de...c_._wpopen.asp for example from Microsoft.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    Oh and if you want to use the > technique, it's fairly easy too
    Code:
    cout << "Enter IP:\n";
    cin >> address;
    cout << "Pinging " << address << "...\n" ;
    mycommand = string("ping ") + address + " > PING.DAT";
    system(mycommand.data());
    
    // Output is now stored in PING.DAT
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    12
    Thanks! I tried this and it doesn't seem to output the file.
    PHP Code:
     cout << "Enter address :\n"
    cin >> address;
    cout << "Pinging " << address << "...\n" ;
    mycommand "ping " address " > ping.dat";
    system(mycommand.data()); 
    Any ideas?
    Last edited by dcj1978; September 8th, 2003 at 09:43 AM.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    12
    Ok..I noticed my problem in that last bit of code. Now, how would I append in the .dat file? would I somehow use the ios::app? I tried this...

    PHP Code:
    mycommand string("ping ") + target " > PING.DAT"ios::app
    And it still writes to the file but it doesn't append.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    68
    Rep Power
    12
    Code:
    mycommand = string("ping ") + target + " >> PING.DAT";
    The >> command typically means to redirect to a file and append to it on the command line.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    12
    Perfect, thanks!

IMN logo majestic logo threadwatch logo seochat tools logo