C++ system( ) tutorial
C++ system( ) tutorial
This is my first tutorial on C++, and I hope that this helps out some people. I will be talking about the system( ) command in C++
1.1 WHAT IS SYSTEM ( )?
In C++, the system ( ) allows your to add system commands in your C++ source. When I say “system commandsâ€, and I am talking about commands such as MS-DOS commands (ping, netsh, ipconfig). The system ( ) also works with linux commands, but you must be on a computer running linux to use them (I will primarily be talking about Windows system commands).
1.2 SYNTAX OF THE SYSTEM ( )
Below is the basic syntax of the C++ system ( ):
#include using namespace std;
int main () { system(“ANY VALID SYSTEM COMMAND HEREâ€); }
Keep in mind that your system command needs to be within the quotations. There really isn’t that much to system commands (other than putting in system commands in the system ( )), but I will give an example of how you could incorporate the system ( ) in your C++ code:
#include #include using namespace std;
int main () { ofstream out ("file1.txt");
out << \"Hello World!\";
system(\"copy file1.txt file1.txt\");
return 0;
}
the above code is an example of how you could make a file in C++, and then copy that file using the system command.
SySTeM 18 years ago
Cool article, the best thing with system is this: system("shutdown -c \"Owned.\" -s -t 01"); :D
ghost 18 years ago
thanks ;) i think that i will eventually add more if i need to, but for now i think that it is good enough ;)
ghost 18 years ago
This article was pretty poorly written and didn't provide a lot of information on the actual beneficial uses of using system(), which are close to none I might add since it isn't portable.
ghost 18 years ago
there isnt a whole lot that you could write about with the system () command, and it mostly depends on what the people who are using system() want to do. I just gave one example, but there could more depending on what the person wants to do.
ghost 18 years ago
… this article is 40 lines too long. system(cmd) was enough, hbh isn't a c++ manual you hsould have explained thats it's near useless unless you are using in for your own stuff
ghost 18 years ago
i can't comment on the quality of this article. But i just wan't to express my thanks to the author b'coz i didnt' knew about this command before.
ghost 18 years ago
remotec2: try to write your own copy program, and don't use system command for that ;-)
ghost 15 years ago
it explains the really really basics… but i always use: #include <stdlib.h> always thought that you needed that for system()
still seems ok,
ghost 14 years ago
It's bad practice, you should just take the time to learn the WinAPI alternative. Another reason to use an OSes API as opposed to this is that system() has to be interpreted by the command interpreter which then calls the correct function. In other words, it causes a crap load of overhead.
ghost 14 years ago
couldn't you use a macro to make this command portable, eg: #ifdef WIN32 #define x strcat("del ", argv[1]) #endif #ifdef NIX #define x strcat("rm ", argv[1]) #endif … system(#x);
then compile for windows with -D WIN32 or *nix with -D NIX? not that I'm endorsing the system command, I've never used it for anything, except a fail attempt to use the clear command, and as it didn't work, I reworked the program to not need it… (and I definitely did not write an entire line of \n… <.< )