how to Read Write on console from GUI c++ - c++

i am developing a GUI app that will create a child process say cmd.exe and write command to it and read output from it. It looks like this
string str;
CreateProcess("cmd.exe"); // child process from GUI
str=writeCmd("dir"); // giving command to cmd.exe and returning output
cout << str;
str=writeCmd("ctrl+z");
cout << str;
if it is possible using using conin$ and conout$ please give me an example.

Related

Read information that has been couted to the terminal

I am trying to figure out how to read information from the terminal or console, (I am using ubuntu)
I am not looking for cin, or readline.
I don't want user input, instead I want a program that will read text that is written in the terminal by cout, put_char etc... How can this information be accessed by a C++ program?
edit:
say I have a program that
int main(...)
{
cout << blahblahblah;
....
}
how can I read the information that was sent to the terminal?
int someFunc()
{
someIostream terminal;
string = terminal.readline();
}
Or as another example
suppose I have some user who has been running things on their terminal
and I want to see everything they typed in before running my program

Reading input from .exe and writing to a .exe for reading Chess engine commands

I am searching all day for an example c++ program which will use ready .exe file which has as an output strings and then waits for standart input and again prints outputs and so on and so forth.
For example my c++ program will use standard output to write "uci" to a .exe program, the .exe program will reply with a string again which I will be able to read in my c++ program and again I will send a new string and will wait for a reply by the .exe.
I found something about pipes but I thought they were really difficult to understand.Is there any ready library/interface I could use? Or any example you can give me with pipes?
If you just know basics of c++ may be you should follow this as it does not require any external libs, though some say system is evil, its okay if it doesn't go to production level programs
int main()
{
std::string in;
while(std::cin >> in)
{
std::string cmd = std::string("/full/path/to/second.exe <") + in + " >outfile.txt";
system(cmd.c_str());
std::ifstream fin("outfile.txt");
std::cout << fin;
}
}
If you open to use bigger frameworks, there is an easy to use class in Qt to handle processes: http://doc.qt.io/qt-5/qprocess.html.
QProcess exe;
exe.start("foo.exe");
exe.write("uci");
exe.waitForReadyRead();
auto result = exe.readAll();
On windows you can use CreateProcess/CreatePipe, but the code will be a lot more verbose. Example:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499%28v=vs.85%29.aspx

Trouble with getline when using eclipse's console

I recently started to use eclipse for my C++ code, and wanted to run a simple function that asks the user for a filename, then reads from the file and sums up all the numbers to test it out.
string filename;
do {
cout << "Enter File Name: "; // Asks for user input
getline(cin, filename);
}
while (filename.empty());
ifstream myfile (filename);
if (myfile.is_open()) {
// sums up text file & outputs it
}
else {
cout << "Error Opening " << filename << endl;
}
The program works perfectly when I run it via Windows Command Prompt in my executable folder and type in the filename. However, when I try to run it via eclipse's console using the exact same input, where I've set the working directory to the project root where the file most definitely exists, the file does not open.
If I change a single line:
ifstream myfile("test1.txt");
Then the code works again perfectly. So now I've narrowed down that for some reason, the Eclipse console interprets my text input via getline differently from the windows command prompt, but I'm not sure what I can do so it behaves consistently.
Calling
cin.ignore()
Before calling getline() just removes the first character of my input (so it tries to "est1.txt") instead.
Any idea what exactly eclipse does to its console that makes getline unreliable? And if this is the case, what I should do to work around it?

Launch console application which uses environment variables from QT Gui application

I am currently making a GUI using QT4.8 which basically needs to launch a console application. However, because this console application tries to fetch some environment variables, I can't seem to manage to make this work.
I am using QProcess obviously and have tried several solutions :
process->start("./yarpbridge", QStringList() << "--from" << "tmp.ini");
This solution does not spawn a console window and besides, by redirecting the output to qDebug(), it prints the erros corresponding to the lack of environment variables.
process->start("gnome-terminal", QStringList() << "-e" << "zsh" << "-c" << "\"./yarpbridge --from tmp.ini"\");
This solution does launch a console window but it nevertheless displays the error messages because somehow .zshrc was probably not consulted when opening the console window.
Would you have a solution that would allow me to do this, and even better that would not only work with "gnome-terminal" and "zsh" users ?
Thanks a lot,
Can you post the error you are getting?
It is very strange because you don't need to start a terminal in order to run a CLI program, maybe after posting your error message I might get an idea what the problem is.
Also you can try this as well:
#include <stdio.h>
char buffer[1024];
FILE* fd = popen("/path/to/yarpbridge", "r");
if (fd == NULL) {
// Error: do something
}
while(NULL != fgets(buffer, sizeof(buffer), fd)) {
QString s(buffer);
s = s.stripWhiteSpace();
// s contains the output, pretty much as readAllStandardOutput() in QProcess
}
// don't forget to close file.
close (fd);

how can I run a .exe file through C++?

My .exe application needs to be opened in console window. Then I have to type the name of a .txt in the console application for it to read. How can I perform all these within my code at once?
I used to be able to run it (and not making it to read .txt) with system("name.exe"), but suddenly it gives,
error: input "name.exe" is not a valid windows application
You can make your application read the filename as a command-line parameter, then you can run your application using system("name.exe name.txt").
In Windows use the CreateProcess API to do this - http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx.
Do not use system.
1- type "name.txt" 2- press enter 3- type name2.exe 4- Press enter. How can I do that through my c++ code?
std::string appName, fileName
std::cin >> appName >> fileName;
system(std::string(appName + " " + filename).c_str())
If names contain spaces, you might need to use getline to read them instead of >>.
--EDIT--
Basically I want once my application is called the input is also automatically given to it.
Creating Child process with redirected input (msdn)
_popen documentation(msdn)
pipe to subprocess (GNU)