Hi all !
Here is my problem...
I'm trying to call a .exe program which I don't have the sources. Only the compiled .exe
I'd like to call it in C but C++ might be used if necessary.
I know that I can use the command :
system("myProgram.exe");
system("pause");
or whatever to execute the program and it works well. However I need to get the output of the program.
For instance if I type system("getmac") I'd like to have the lines in a string array (line by line).
I read about external files to transfer the outputs but this "system call" will occur more than a thousand times / second and I don't want to slow my simulator with HDD files exchanges.
I'm under windows so Unix Pipes (Popen and co...) wouldn't work...
Do you have any clues on how to get this working ? Thx a lot !
C++ program that gets another program output from console to a string array.?
The correct and portable answer is the often forgotten read() and write() from the CSL and CRT. Not only will that work on any hosted compiler on any platform, but using things with such amazingly straightforward names will make you look sexy to the maintenance coders.
You really should read up on their use, first, though (Josuttis' reference on the Standard Template Library goes into excellent detail, since these are used to implement iostreams and other hairy parts of the C++ underbelly.) There's some fairly odd stuff you have to know about the buffering and blocking behavior with all CRT IO stuff in C/C++ if you want it to be portable.
Surprisingly, the clearest and most straightforward examples I'm aware of regarding the use of read() and write() come from a different language entirely - the Erlang manual has a radically simple use of read() and write() when explaining how to open named ports (an Erlang feature.)
You can find the C example here (it's C++ safe too) :
http://www.erlang.org/doc/tutorial/c_por...
If you feel like hooking a brother up over his amazon referral link, hit this to get the aforementioned book: http://sc.tri-bit.com/JosuttisSTL
Reply:You can use pipes in Windows, but the API is a little different. Essentially, this will be doing file I/O but I think you can re-route stdout to your program's stdin.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment