I'm new to C++, and I've read and heard that using system calls is bad practice. So what if you need to use system() to run a Windows command like ipconfig
or netstat
. Is it still evil or is that considered an acceptable use of system()? I'm writing a program to collect a variety of information about the system and I use system() many times and pipe the output into text files for review.
Using system(), we can execute any command that can run on terminal if operating system allows. For example, we can call system(“dir”) on Windows and system(“ls”) to list contents of a directory.
System() Function in C/C++ It is used to pass the commands that can be executed in the command processor or the terminal of the operating system, and finally returns the command after it has been completed. <stdlib. h> or <cstdlib> should be included to call this function.
system() returns the exit code of the process you start.
Most of the calls you'd make have Windows API calls you can use directly. Ideally, you'd just call the appropriate Windows API call instead of launching a process, writing to a file, then parsing the file.
That being said, on Windows, CreateProcess
will launch a process and provide you a lot more control than a call to system
. This includes using STARTUPINFO
to provide your own HANDLE
for the process standard output stream, which means you can directly read from the process without writing to a file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With