I have an application that need to send command to cmd and then get the ouput back (capturing the output). How can this be accomplished using C++ without using any MS Windows specific API? Is there a way that this may be done to be cross platform (for linux terminals for example)?. By the way i'm on win XP SP3.
I actually mean redirecting the input/output. For example, run the command "make" on cmd and then in case of error capturing the error message (redirecting to my application).
As mentioned: if you can avoid launching child processes in your program and instead fit into the broader "toolbox metaphor," that can often be better...
http://en.wikipedia.org/wiki/Unix_philosophy
But if that's not a fit for your project, check out Boost.Process.
Also: if you're using Qt (which is good to look at in any case) there is also QProcess.
Can't you just use the regular cin and cout that C++ provides? (of course if your program is a GUI program, cin and cout won't be connected to anything useful unless you call the Windows AllocConsole() command... but that's just the way Windows works. If you want code that also compiles under Linux, etc, you can put #ifdef WIN32 around that call)
Well the system() function which is part of the C89 and C99 standards is available on Linux and Windows and allows for command execution inside C / C++.
By default, most systems get their standard input from the keyboard, therefore cin is generally expected to get information from the user, although there are many cases where this can be redirected to some other source.
You can do something like:
cout << "How old are you?" << endl;
int age;
cin >> age;
Related
I'm trying to have a separate console window for my program that is already in a console. How would I be able to open this new console window then output to that specific console?
I've found ways to do it that work in windows using "cconsolelogger", but not for Linux.
I assume that by "new console", you mean a terminal emulator window.
A terminal is a program like any other, so you start it like any other program. The only standard way in C++ to open another program is std::system, which executes a shell command. Here is an example of opening a terminal emulator:
std::system("xterm");
Note that it is not safe to pass arbitrary user provided input into the command, because it is vulnerable to shell injection.
The POSIX standard - that is followed by Linux operating systems in general - provides other, lower level tools to run another executable. In particular the exec family of functions allows executing another program without starting a sub process and without involving the shell.
Maybe you can use popen.
You can choose the program to write command to it, for example:
gp = _popen("C:\Program Files (x86)\gnuplot\bin\pgnuplot.exe", "w");
fprintf(gp, "set term png\n");
I have an console application 'app.exe', which i want to invoke from a C++ program and then communicate with it as if it was a command line. Essentially I want to make a C++ wrapper around another console application so that I could pass input to it at will and receive output.
In pseudo-code something like:
std:string input("...some parameters..."), output;
Process app("app.exe");
app.InputOutput(input, output);
std::cout<<output;
This must have been answered already, but I seem to lack proper terminology to look it up.
In case it matters, I am running Eclipse CDT on Windows 10 with GCC 5.3.0
EDIT: I need to be able to repeatedly send some values to 'app.exe' and repeatedly receive response, rather than just invoke it with parameters. This is needed for a small personal project so I do not care about it being platform-specific.
I used this code as a starting point, in an MFC dialog, to display output from a called process. It was rather painless as this is well documented. He tells you why he is doing what. It should be suitable as you are working with the Windows platform. But as Alf points out, cross platform is something else.
You can use the system function to invoke a shell (command line) command.
That command can be to execute a program with the arguments you want.
system returns the process exit code, but for other results there is no direct support. One easy way to access the output, for a program that just does a job and ends, is to redirect the program's output to a file.
Otherwise you'll have to use communication mechanisms such as pipes or Windows mailslots, that are not supported by the C++ standard library, i.e. you're then into platform-specific code.
I know there have been many work around proposed for immediate shutdown, but I was wondering if there is other way to do so for cross platform?
I think system("pause") is visual studio / windows specific and getchar() or other similar stuff that waits for users' input create unnecessary input for exiting the program running under say gcc.
Any idea?
-- Edit --
I also tried hitting Ctrl+F5, but it doesn't work sometimes. So I'm looking for an alternative command (if there's any) or setup that can pause the console screen in visual studio and doesn't cause any discrepancy in other c++ compilers.
This problem only occurs when you launch a console program from a GUI. So there is a very simple cross-platform workaround -- run console programs from a console. If you want to make a program that runs well from a GUI, make a GUI program.
The other suggested workarounds are awful. Both getchar() and system("pause") interfere with any attempt to use the program as a filter or to redirect its input and output. It doesn't make sense to break a program so that it works "correctly" when used incorrectly.
u can use this method
after that u write the last code of your program(before return 0;) u can use for example ( cin >> x; ) command..
then the program will wait for u to enter new data. and u can see your answer in debugging program :D..
good luck with this trick :D
I am writing an application for a robot.
The required UI for the application is described in the pseudo-code below:
while(true){
if (spacebar is not pressed){
//do something
}
else{
sleep(1); //wait for a second
}
}
If I use cin or some other console input reading function then it will wait for user to press something. How do I ensure that it does not wait to get any input?
I am using Ubuntu. But I do not want it to be OS-specific.
Answers here seem to be OS specific.
Terminal Level input
What you are asking for is fairly close to the hardware (key-press / key-release) compared to the "standard input/output" stream concepts. So your implementation would have to be OS specific. Having said that the library to use is curses[1] which has been around for a long time and is standard on a lot of Un*x platforms. The GNU ncurses flavor compiles for pretty much all of them, it is a standard install in almost all Linux environments, and where it isn't installed by default you can find it. It also works well in Windows (cygwin), os/2 and a bunch of embedded systems etc. so you should be able to write a fairly portable software using curses that does what you want.
It's not too clear what you're asking for. In the if, is the
condition based on whether a space character has been entered,
or whether the user is currently holding down the space bar? In
the first case, you need something like curses (a portable
library which does gets each character as it was entered,
without echo). In the second, I don't think that there is
a portable solution. Even non-portably, you might not be able
to get it if your program is reading from a terminal window
(e.g. xterm); this sort of information is typically only present
as window events, when you created the window in your program.
I'm trying to make a C++ console application using Xcode 4.1, but I can't find the command for cleaning the screen while the program is executing...
Any ideas? Thanks!
OSX doesn't have "consoles" the way Windows does. It has pseudoterminals, which act like an old-fashioned glass terminal to the program running "inside" them, and like a bidirectional pipe to the program that set them up. That outer program can do whatever it likes with the inner program's input and output. Notable examples of such programs are Terminal.app, which emulates the venerable VT-100, and ssh, which forwards the I/O over a secure channel to its own controlling terminal (which is probably itself a pseudoterminal). This is all by way of saying that there isn't a method that's guaranteed to work, because maybe the program on the outside of the pseudoterminal doesn't have a "screen" that you can meaningfully "clear." (Expect is a good example of a program like that.)
Having said that, though, if there is a screen, these days you can pretty much count on it to respect the VT-100 control codes. So this should do what you want:
std::cout << "\033[2J" << std::flush;
If you find that you need even one more control code, though, it's time to hook your program up to ncurses, which presents a nice friendly API to all the tricks that modern terminal windows are capable of, and will also have your back in the increasingly unlikely event that your program is attached to a terminal (or a program emulating a terminal) that is not a VT-100 nor one of its descendants.