Quick question about system routines

This is a discussion on Quick question about system routines within the C++ Programming forums, part of the General Programming Boards category; I'm a noob... as you can tell. Wondering if anyone can give me a hand... Is there any way to ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    11

    Quick question about system routines

    I'm a noob... as you can tell.

    Wondering if anyone can give me a hand...

    Is there any way to put variables into a system routine? I think that's what they are called.

    I'm trying to do a net use command in dos

    system("net use * \\\\0.0.0.0\\c$ * /user:administrator");

    I would like to make 0.0.0.0 a variable, and the *a variable and the username one too?

    Does anyone have any ideas?

    thank you for even looking at this.

    Chris

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    This what you meant?
    Code:
    int main(int ArgCount, char* ArgList[])
    {
       cout << ArgList[1] << endl; // net
       cout << ArgList[2] << endl; // *
       cout << ArgList[3] << endl; // \\\\0.0.0.0\\c$
       cout << ArgList[4] << endl; // *
       cout << ArgList[5] << endl; // /user:administrator
    
       return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    If you want to build a string from variables, you can use ostringstream:
    Code:
    ostringstream ostr;
    int ip[4] = {0,0,0,0};
    string password = "*";
    
    ostr << "net use * \\\\" 
         << ip[0] << '.' << ip[1] << '.' << ip[2] << '.' << ip[3]
         << "\\c$ " << password << " /user:administrator" << ends;
        
    cout << ostr.str() << endl;
    Notice the "ends" at the end - this is the "null-terminator" which is needed if you want to use it as a C-string by calling str().

    gg

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    Hey,

    Thanks for the info guys. I will give them a try.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    ostringstream ostr;
    int ip[4] = {0,0,0,0};
    string password = "*";

    i tried to use the following and it keeps hanging here. Dug around the intenet so I wouldn't be pain, but didn't find anything that could help.

    Is there anything I need to declare for set these commands to make them work properly.

    ostr is the output?

    The rest of the code I understand this is the only thing that I' hung up on.

    Chris

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    What "hangs"? The compile?

    You need some headers to access the string and ostringstream types:
    Code:
    #include <string>
    #include <sstream>
    using namespace std;
    If you must use old school C++ then use ostrstream:
    Code:
    #include <iostream.h>
    #include <strstrea.h>
    ...
    ostrstream ostr;
    int ip[4] = {0,0,0,0};
    char *password = "*";
    
    ostr << "net use * \\\\" 
         << ip[0] << '.' << ip[1] << '.' << ip[2] << '.' << ip[3]
         << "\\c$ " << password << " /user:administrator" << ends;
        
    cout << ostr.str() << endl;
    I suggest you use/learn the current standard.

    gg

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    Thanks for all your help...

    I think I'm just a bit slow from time to time.

    Old standard? I'm using microsoft c++ of a select cd from '01. Is that old? i agree with you, learning the newest would be beneficial. Are you speaking of C#?

    And yes, the compiler hangs.

    Just tried it, worked fine. Now it's makes sense...
    Last edited by christopher15; 03-21-2003 at 01:41 PM.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    C# is a completely different language.

    There is the current C++ standard library and the old. I gave you examples using both. A microsoft compiler for '01 should be able to compile both versions, but you shouldn't mix.

    If you are still having troubles compiling your code, the post your code, or post the actually compiler errors.

    gg

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    I'll try using the newer version for my next "slaughtering" of the langauge.

    The string that you helped me with displays the info on the screen, but it doesn't "run" in dos. I tried using the system ()command with it, but asyou would expect with no luck.

    I can't get a variable to run with it. I there a way to make the program do that dos command without interfacing with dos, because I don't think that wll work, maybe I'm wrong though.

    Basically I'm starting to understand the syntax and langauge, but how it make the computer work on command I'm a bit confused on.

    I can get it to add and subtract and understand that, but anything more is a bit out there for me.

    Any good webpage or explainations so I don't keep bother y'all?

    Chris

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    system() should work just as if you typed in the parameter at the dos prompt. For example, system("dir") will perform a dir just as if you typed it in at the dos prompt. If you system() call isn't working, try typing in the command at the dos prompt, perhaps the command is quite right.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  2. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  3. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  4. quick question about C Dos text menu pgm i was doing
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 09-16-2001, 10:26 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21