Archived

This topic is now archived and is closed to further replies.

system("")

This topic is 4960 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

can someone explain what the system("") function is for in C++ because I use system("PAUSE") to stop programs from just closing when you open them but today I saw a program with system("Hello"). I read a little about it in thinking in C++ but I still don''t get it. Please explain what this function is used for.

Share this post


Link to post
Share on other sites
system spawns an executable with the specified name, relying on the user path to resolve exactly which binary is invoked. system("pause") calls the pause executable - but it doesn't exist on Unix systems! Furthermore, if a malicious individual is able to place a binary named pause anywhere closer on the path, such as in the same directory as the spawning binary, that unknown binary will be executed instead, enabling the intruder perform any action desired.

Now that you know what it does, you should be able to figure out that it's bad practice. Most people use it because the Dev-C++ environment doesn't delay closing console applications; either execute the binary manually (ugly), add any blocking operation to the end of your application (bad), or switch IDEs (good).

Seriously.

[Edit: Typo.]

[edited by - Oluseyi on May 20, 2004 1:12:10 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
system spawns an executable with the specified name

That''s not accurate. system() runs a shell command, like you''d type in the Unix terminal or DOS console. So I believe you can run system("wc | ls") in Unix, for example, or system("cd\games") in DOS.

Unix does not have any commands built into the shell, so technically every system() call would end up running a program... but it''s not like fork/exec.

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..

Share this post


Link to post
Share on other sites
Just a note that you can''t use system("cd \\games"); to change directory of your process; the system runs the shell as a sub-process which has its own working directory, and will not affect yours.

Note that if you''d even wanted to do that (which wouldn''t work), you would have to escape the backslash as above.

Slarty

Share this post


Link to post
Share on other sites
quote:
Original post by CGameProgrammer
Unix does not have any commands built into the shell, so technically every system() call would end up running a program... but it''s not like fork/exec.


Uhm, hm... ever looked at man sh? There''s a whole section dedicated to "Builtin commands". alias, for example, is built into the shell.

Share this post


Link to post
Share on other sites
Whatever OS you''re on, system() spawns a shell and runs the command on there. How the shell implements this is up to it.

On Windows the shell is cmd.exe and has a lot of the old DOS commands implemented in it, like copy, rename, delete etc. But not every command exists in this form, so it has to spawn a subprocess for it.

So in fact, your program runs the shell, which then MAY run another program in turn, if the command is not a built-in.

Mark

Share this post


Link to post
Share on other sites
For Windows at least, system("something") does exactly what would happen if you opened an MS-DOS prompt in the same directory as where your .exe is and typed something there. Try openning a DOS prompt and typing pause for example, see what happens.

For Unix, you can start a shell process and use the -c option to specify a command that includes wildcards, piping, etc. For example, system("sh -c \"cat *.cpp | grep // > comments.txt\""); would take all lines that start with // from all your C++ files and put them in comments.txt. (Amazingly useful, of course..)

[edited by - Matei on May 20, 2004 8:20:15 AM]

[edited by - Matei on May 20, 2004 8:21:07 AM]

[edited by - Matei on May 20, 2004 8:23:37 AM]

Share this post


Link to post
Share on other sites
quote:
Now that you know what it does, you should be able to figure out that it''s bad practice. Most people use it because the Dev-C++ environment doesn''t delay closing console applications; either execute the binary manually (ugly), add any blocking operation to the end of your application (bad), or switch IDEs (good).


Console apps automatically close on exit under all WinNT based systems. Dev does it, VC++ does it, running it manually does it. (Under Windows anyway)

Just out of intrest, what would you recommend as an alternative IDE? (Thats not a sarky comment, I am actually interested now )

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
either execute the binary manually (ugly), add any blocking operation to the end of your application (bad), or switch IDEs (good).


You forgot "make a batch file/shell script" (better).

e.g. whatever.bat
myprogram.exe
pause



“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
quote:
Original post by zoggo
Console apps automatically close on exit under all WinNT based systems. Dev does it, VC++ does it, running it manually does it. (Under Windows anyway)
You missed the point; perhaps I was too subtle. VC++ terminates the app, but keeps the console window open until you press a key. Running it manually, you''re already at the CLI, so the window isn''t closed until you exit (I wasn''t suggesting using Start -> Run...).

quote:
Just out of intrest, what would you recommend as an alternative IDE? (Thats not a sarky comment, I am actually interested now )
If Dev-C++ hasn''t seen it fit to address this obviously significant concern, given the number of questions about it, in five versions, maybe it''s time to move on to something else. MinGW Developer Studio handles it as gracefully as MSVC; an IDE that isn''t simultaneously making development easier and promoting good practice is a bad IDE.

Besides, I just don''t like Dev-C++. It feels... shoddy.

Share this post


Link to post
Share on other sites
quote:
You missed the point; perhaps I was too subtle

Patronising bastard

Anyway, is there an option I''m missing under VC? I''m running Win2k and console apps always close as soon as they exit on my system.

quote:
Besides, I just don''t like Dev-C++. It feels... shoddy.


Can''t say I''m that keen on it myself. Mind you, I don''t like MSV that much either. I prefer to edit using notepad sometimes, (Masocism, I know) however I always miss the ease of debugger use from the command line.

Share this post


Link to post
Share on other sites
quote:
Original post by zoggo
Anyway, is there an option I''m missing under VC? I''m running Win2k and console apps always close as soon as they exit on my system.

VC leaves them open if you "Start without debugging". If you run the app under the debugger, it is probably assumed that you have breakpoints and stuff in there anyway.

Share this post


Link to post
Share on other sites
quote:
If you run the app under the debugger, it is probably assumed that you have breakpoints and stuff in there anyway.


Hmm. Trouble is, all I want is to view stuff output to the console after the app has exited. Oh well, I''ll just stick to my getch() call at the end of my app. (Bit of a bodge, but who cares? It compiles without it in release builds)

Share this post


Link to post
Share on other sites
I wasn''t referring to Debug vs Release builds. I was referring to pressing F5(Start) vs pressing Ctrl-F5(Start without debugging). The latter won''t attach the debugger to the new process, but it will leave the console window open after the process exits.

Share this post


Link to post
Share on other sites