usage of system() function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • snehil
    New Member
    • Jun 2009
    • 19

    usage of system() function

    Can I use this code to open Internet explorer if i compile the program file and use the .exe file of the program if yes then pls also tell me the header file..
    Code:
    system("C:\Program Files\Internet Explorer\IEXPLORE.EXE");
    I was trying to make this code but it is not working as I want it to work.. The compiled file displays "Bad command or file name"
    Code:
    #include<stdlib.h>
    void main(void)
    {
    for(;;)
    {
    system("C:\Program Files\Internet Explorer\IEXPLORE.EXE");
    
    }
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Review the documentation for system. (These are general code hygiene suggestions; they probably won't lead to a solution to your problem.)
    • It has a return value that you should be checking.
    • Consider whether its behavior when passed NULL would be helpful to you.



    What happens when you execute your system command string in a Command Prompt window?
    Code:
    C:\Documents and Settings\snehil>  C:\Program Files\Internet Explorer\IEXPLORE.EXE

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      It's also possible you'll have to escape your backslashes in the command (you have to do this in other languages I know).

      Mark.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        It is more than possible, it is necessary. As with many other C like languages in C the character '\' has a special meaning inside a string, it is the escape sequence. Depending on what follows it made have a special meanins, i.e. '\n' is the newline character, '\0' is the zero character (the terminator). If it has no special meaning then it take is ignored '\q' is the character 'q'.

        If you want a \ character then you need the sequence '\\'.

        Comment

        • snehil
          New Member
          • Jun 2009
          • 19

          #5
          ok.. now the code is
          Code:
          #include<stdlib.h>
          void main(void)
          {
          for(;;)
          {
          system("C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE");
          
          }
          }
          but still the same problem:

          and at donbock.. when I execute the command in command prompt
          this is the output..



          i dont have much knowledge of advanced programing as I am still a beginer...
          I saw this code on net and I am just keen to know if it works or not...
          from where I saw tyhis code header file was not there so pls also tell if this header file is correct...
          bcoz maybe system function is also in process.h m not sure though...

          Comment

          • newb16
            Contributor
            • Jul 2008
            • 687

            #6
            There's a thread about 'execute php' at 12 jul. Error message "C:\program is not recognized" gives you a hint - the space is interpreted as a separator, enclose the full file name in quotes. - system("\"C:\\P r...

            Comment

            • george666
              New Member
              • Jul 2008
              • 28

              #7
              system() is prohibited (and dirty) on Windows (see MSDN)
              use Shell apis.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Also not true.

                For Visual C++ 2008, the latest version there are the following pages on the system function

                Implemetation Defined Behaviour

                system, _wsystem - the actual help on the function

                In neither of those 2 places does it say that system is prohibited. Further more Visual C++ 2008 is an ANSI C compliant compiler and to be such it has to fully implement the Standard C Library that includes the system() function.


                If you want to make statements like that you need to back them with hard evidence such as a link or quote.

                Comment

                • snehil
                  New Member
                  • Jun 2009
                  • 19

                  #9
                  but i still havnt found wat I ws looking for....

                  Comment

                  • lumpybanana247
                    New Member
                    • Apr 2007
                    • 134

                    #10
                    yeah, you can launch internet explorer.
                    your code was about right...


                    1st. main is supposed to be an int (not void) and should return a number (such as 0).
                    2nd. a "\" in a string must be written as "\\"
                    3rd. in many systems (well, mine) you must make the directory into "Shortfilen ame" because it does not contain spaces.
                    ex. C:\\PROGRA~1\\I NTERN~1\\IEXPLO RE.EXE
                    but yes, your code holds the right idea

                    PS. your header worked fine for me.

                    Comment

                    • snehil
                      New Member
                      • Jun 2009
                      • 19

                      #11
                      thanx.. it worked like a charm.. :)
                      ok 1 more query...
                      in this code... IE again opens when i close one window of IE...
                      is there a code to forcefully open multiple instances of IE window at a same time in an infinite loop... ?

                      Comment

                      • lumpybanana247
                        New Member
                        • Apr 2007
                        • 134

                        #12
                        yes, but it will run so fast that it will make your computer flip in a second. so do not run it without saving all data first. i repeat that: DO NOT RUN IT WITHOUT SAVING ALL DATA
                        but instead of the system function, i would recommend ShellExecute. it requires <windows.h>
                        here is an example
                        ShellExecute( NULL, "open", "C:\\Progra~1\\ Intern~1\\IEXPL ORE.EXE","","C: \\", SW_SHOW);
                        This is much more versatile.
                        -you do not need to use ShortFileName. Program Files\Internet Explorer works perfectly fine.
                        -arguments for the program-to-be-run are accepted in the argument i left blank.
                        -also, you can do SW_SHOW: window is shown; SW_HIDE: window is hidden; etc

                        Comment

                        Working...