Thread: System () function help

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    11

    System () function help

    Hi,


    I wanted to know whether it is possible to use the system() function to execute DOS commands when using DEV compiler, if yes please let me know how ?

    Reagards,
    Vinith

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Of course. You type system("command you want to happen"). system is found in <stdlib.h>, so be sure to include that header.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Sometimes its kind of cool to just compile code to see what happens. There is a bliss involved with trying out something just to see if it will work and seeing that it does in fact work as you planned. I say this with no sarcasm intended, but don't let us rob you of the joy of discovery.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    He may not have "DEV compiler" at hand to experiment. I certainly don't so I can't be 100&#37; sure system() is available there.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    system() is a standard C function, though. It should be there.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Is it? Can the standard for C dictate that a functioning DOS shell be available? For example on some minimal embedded controller?

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Good question. The answer is no. The standard simply dictates which functions should be available and how they should work. Not specifically how they need to work internally or how they are externally manifested. So though your system() function is standard. The parameter string that you pass into that function may be entirely implementation specific or platform specific.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Then you agree that your answer to vinith98 was made using assumptions as opposed to certain knowledge about "DEV compiler", bacause, at best, we have no idea what the target executing environment will be.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >> system() function to execute DOS commands

    ho ho... You tricky guy! Nice catch. Alright nonoob, you get to wear the official "Mr. Observant" sombrero. People will be able to behold your cunning from great distances.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If I go out on a limb with another assumption and say he is probably meaning he is using the Dev-C++ IDE (which isn't even a compiler) then I can safely say that my assuming the rest is fair since Dev-C++ is for windows exclusively.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Oh I see. You overlooked the OP's "to execute DOS commands" but instead cited the function system()'s existence as part of ANSI standard regardless. Fair enough. You are likely correct (I haven't looked up the standard).

    I took them to be one and the same... a package if you will. I guess under some systems and circumstances, the system() function can accept any string/word/token commands and then execute whatever the prevailing system shell was, or emulated... even if the commands were "XAGD$&#37;$$G" to list the directory, or whatever.

    I wasn't looking for people to behold my cunning at great distances. Although the prospect seems alluring.
    Last edited by nonoob; 10-06-2008 at 07:10 PM.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well I guess we both need to pay a little more attention to original posters... Its easier to get more involved with the wild tangents the thread has steered off into than to address the needs of the poor student who is frantically trying to get their code to compile. Though in this case the OP should have just compiled a program something like this:

    Example:
    Code:
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
      char buffer[4096];
    
      while(fgets(buffer, sizeof(buffer), stdin))
      {
        strtok(buffer, "\n");
    
        if(!strcmp(buffer, "quit"))
          return EXIT_SUCCESS;
    
        system(buffer);
      }
    
      return EXIT_SUCCESS;
    }

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Novel use of strtok. I thought the manual said it returned the token... I never thought it would mash the contents of the original buffer in the process... (in this case, put a '\0' where the '\n' was).

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah I don't remember who I learned that technique from, but it has served me well over the years and it was definitely something I picked up here. I am wanting to say Prelude got me doing that. Either her or Cornedbee. One of those lofty girls... I remember that much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. measuring system resources used by a function
    By Aran in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 05:35 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM