Thread: Invoking system commands... "A better way?"

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    6

    Invoking system commands... "A better way?"

    Hey... I always hear that it's considered bad to use system commands. It seems like you sometimes have to use a dos or unix command! However, I have also noticed that you can't use a string variable to call the system command.

    Gives an error:
    Code:
    #include <stdlib.h>
    #include <iostream.h>
    
    int main()
    {
    char SystemCommand[] = "copy C:\file.txt C:\newfolder\file.txt";
    system(SystemCommand);
    return 0;
    }
    Doesn't give an error:
    Code:
    #include <stdlib.h>
    #include <iostream.h>
    
    int main()
    {
    system("copy C:\file.txt C:\newfolder\file.txt");
    return 0;
    }
    Is there a way to use a string variable to call a system command, like I wanted to in the first code block?

    Thanks,
    The_Muffin_Man

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Hmmmm compiles for me with this change
    Code:
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    char SystemCommand[] = "copy C:\file.txt C:\newfolder\file.txt";
    system(SystemCommand);
    return 0;
    }
    Woop?

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    6
    I'm sorry... but that's wierd. Why would using the ANSI standard header files with declare the namespace std make a drastic difference? Maybe my compiler's header files are messed up. Thanks for helping. Did it work with using the <iostream.h> instead of the <iostream>?

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Nope i got a backwards warning are ya using dev c++?
    Woop?

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    6
    Yes for windows I am. I also use gcc for linux, but I haven't tried it for linux.
    Last edited by The_Muffin_Man; 06-09-2004 at 08:49 PM. Reason: d

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    oh and btw you might want to make your char into a const char* cause thats what system uses
    Code:
    //Taken From Header
    int system(const char*);
    And one more thing for more standards compliant
    Code:
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    Last edited by prog-bman; 06-09-2004 at 09:04 PM.
    Woop?

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    6
    Thanks a million! By the way, is there anything wrong if I wanted to include a non-standard header file like windows.h with ANSI standard header files. Somebody told me that this is bad practice.
    for example:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h> <----- Bob says, " uh oh! That's a bad thing!"

    Is this okay?

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Windows is its own deal if ya want to use it that is the way to.
    but c++ knows nothing of windows so your not standards compliant cause that code won't work on non-windows system(duh)
    Woop?

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    try:
    Code:
    char SystemCommand[] = "copy C:\\file.txt C:\\newfolder\\file.txt"
    And a tip, don't just say it gives an error. Tell us what the error is.

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    6
    oh yeah, I'm sorry about that backslash thing... I'm just so used to linux where I would say:
    Code:
    char SystemCommand[] = "cp /home/file.txt /home/newfolder/file.txt";
    So sorry. That wasn't intended. I was just in a rush. But thanks for your guys suggestions. You people were very helpful.
    Last edited by The_Muffin_Man; 06-09-2004 at 09:20 PM. Reason: d

  11. #11
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    The following is standard C++:

    Code:
      #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    char SystemCommand[] = "copy C:\\file.txt C:\\newfolder\\file.txt";
    system(SystemCommand);
    return 0;
    }
    If this doesn't work, your compiler doesn't follow the standard.

    What was the error message of the first example? Judging from the two examples, your compiler seems strange.

    prog-bman: A (char[]) can be converted to a (const char[]) without any cast.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. System(); commands
    By Ryan in forum C++ Programming
    Replies: 15
    Last Post: 03-15-2002, 07:05 PM
  2. system commands
    By mervin in forum C Programming
    Replies: 3
    Last Post: 02-03-2002, 09:01 AM
  3. DOS Commands
    By Zuul in forum C++ Programming
    Replies: 11
    Last Post: 01-04-2002, 04:41 PM
  4. string variables and system commands part 2
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-25-2001, 12:57 AM
  5. string variables in system commands
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-24-2001, 12:36 PM