I want to run a dos command from my program for example "dir" command. I am doing it like,
system("dir");
Is there any way to read the output of that command directly into a program variable?
We can always redirect the output to a file and then read that file, by doingsystem("dir > command.out");
And then reading command.out file. But how can we do it directly rather than redirectling to a file and then reading?
You can't redirect it to a variable, but you can do a trick similar to how pipes are used in Unix for chaining commands. Call CreateProcess()
, and pass it a STARTUPINFO
instance with accordingly set handles and STARTF_USESTDHANDLES
in STARTUPINFO::dwFlags
. Then read the data coming from the spawned process through the set handles.
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