Thread: Using variables within system command?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    13

    Using variables within system command?

    Hey guys, I'm not sure if what I'm trying to do is possible or the way I should do it. Basically I want to ask for a hostname, a message and place them in string variables as host and msg respectively. The contents will then be parsed onto the "net send" system command.

    This is my best way of describing what I'm trying to do:
    Code:
    system("net send ") << host << msg << endl;
    If its not clear enough I can post more of the code.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Put that into one string and then pass it to the system command.

    Code:
    string systemSender = "net send ";
    
    systemSender += host + msg;
    
    system(systemSender.c_str());
    Woop?

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Though make sure to add spaces between the parameters.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    How do you know it needs spaces . Maybe its comma sepeated
    Woop?

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Because generally if a person is using 2 different varaibles for a command such as that, it will need spaces between them because they are sperate parameters. Though I could be wrong, I doubt that I am.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    It was a joke(just making sure you know that).
    Woop?

  7. #7
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I was just showing off my mind reading skills in my first post in this thread... then I had to back track so people didn't think I could read minds that way I don't get asked... "What am I thinking right now?!"

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    I got a compile error from Dev C++ telling me
    Code:
    systemSender += host + msg;
    is wrong, so I changed it to
    Code:
    systemSender += host, msg;
    and it compiles fine. Everything seems to be working fine except when I try "Host: 127.0.0.1" & "Msg: Hello" it tells me sending files are no longer supported. I think this is because the variables parsed mighten be spaced out after eachother, so its trying to perform this command:
    Code:
    net send 127.0.0.1Hello
    How can I space host and msg out?

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Well, what are host and msg defined as? If the are just char * or char[] then yes that won't work.

    And to add a space you can make those strings and do this:
    Code:
    systemSender += host + " " + msg;
    Woop?

  10. #10
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Commas are not the same as most languages and lists when in programming languages. I am supprised that worked as it did.

    One thing you can do is
    Code:
    string systemSender = "net send " + host + " " + msg;

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    The +'s dont seem to work, I get an error on compile but I'll show you what does work.
    Code:
    #include <iostream>
    #include <dos.h>
    #include <string>
    
    using namespace std;
    
    char host[64], msg[512];
    string systemSender = "net send ";
    
    char getinfo()
    {
         cout<< "Hostname: ";
         cin.getline (host, 64, '\n');
         cout<< "Message: ";
         cin.getline (msg, 512, '\n');
    }
    
    char send()
    {
         systemSender += host;
         systemSender += " ";
         systemSender += msg;
         system(systemSender.c_str());
    }
    
    int main()
    {  
        getinfo();
        send();
        cin.get();
    }
    Anything I'm missing thats not letting me use "+ host + msg" ?

  12. #12
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Yes like I said above the + operator isn't overloaded to take 2 char*'s which is what you are doing there.
    Woop?

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    So what can it take two of?

  14. #14
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by moagim
    Anything I'm missing thats not letting me use "+ host + msg" ?
    Yes, you cannot concatenate character strings like that. The + operator only works if one or both arguments are std::string.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  15. #15
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    Sweet everything works thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the system command
    By happyclown in forum C Programming
    Replies: 7
    Last Post: 01-02-2009, 07:14 AM
  2. Replies: 6
    Last Post: 07-21-2008, 03:10 AM
  3. Replies: 30
    Last Post: 09-12-2006, 07:06 PM
  4. Using Variables with th 'System' command
    By dapernia in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2003, 05:11 PM
  5. System Calls && Variables
    By Okiesmokie in forum C++ Programming
    Replies: 6
    Last Post: 03-06-2002, 09:10 PM