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
Related
Generally we need to type the input after running any file where we have std::cin, like the C++ code in below
#include <iostream>
using namespace std;
int main()
{
// your code goes here
int t;
cin >> t;
}
I just don't like to enter the same input every time. So I want to automate this process for the same input, say I save my input in a file called input.txt and after running the file, it should take the input from input.txt and output the results. Of course saving input to the clipboard is one way but I might want to copy other things while coding and copy-pasting is itself is again one small job.
I use VS code editor in windows and run code in terminal extension.
The solution is to learn to use your shell. I’m assuming a Unix-like shell here.
Type all of your inputs into a file as you would enter them while the program is running. Save it.
When you run your program, use the command a.out < input.txt. Substitute the appropriate names, obviously.
Your program will read the inputs from the file as if they had been typed in.
Note that because nothing was actually typed in, your formatting might look a bit off, but it’s not a big deal compared to the time you’re saving in running your tests.
You could use, following function inside your main function
int main() {
freopen('input.txt', 'r', stdin);
freopen('output.txt', 'w', stdout); //if you want to save output to a file.
/*
... your code
*/
}
I have simple console application that runs in terminal window reads and prints character:
int main(int argc, char **argv, char **envp)
{
while (true)
{
char c =getchar();
printf("%c \n",c);
}
}
Now I would like to make test application that could emulate character press in first application terminal.
Which way I should go? What API functions I should use for this purpose?
No need for special APIs or whatever. Since your sample application is only reading from standard input, you can just send stuff to there.
Before running the program in a terminal, check its connected terminal using tty command. Then send data to that tty that tty reports.
Alternatively, grab the PID of your running application and send data to /proc/$PID/fd/0 so you don't need to check for tty.
Just pipe the test data to your process:
echo "some test data" | ./myprogram
(Your example program in the question will read and print each letter from "some test data").
There are plenty of other variations on this. Read about the shell and shell pipelines.
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
i implemented program of network statistics with help of powershell scrpit. the program is running successfully and giving me perfact output as well . below is my program.
int main(int argc, char *argv[])
{
string strPath = "C:\\Get-NetworkStatistics.ps1";
char str[50] = "C:\\Get-NetworkStatistics.ps1";
char command[500];
//access function:
//The function returns 0 if the file has the given mode.
//The function returns –1 if the named file does not exist or does not have the given mode
if(access(strPath.c_str(),0) == 0)
{
_snprintf(command, sizeof(command), "Start Powershell.exe -noexit Set -executionpolicy;.'%s';Get-NetworkStatistics",str);
system(command);
}
else
{
system("cls");
cout << "File is not exist";
system("pause");
}
return 0;
}
! here is the output of above program
as you can see the output is in the powershell windows.. i want to fetch all this data of powershell output and want to display it in console. how should it possible?
please help me..
Unless you need to do display the info in realtime as it becomes available, just redirect it to a file, then read that file from C++.
Since netstat was lobotomized in Windows XP SP 2 or thereabouts I can understand using Powershell.
But it may just be that netstat will serve your needs, and then you don't have to deal with any of that complication.
By the way, I recommend using a scripting language for scripting tasks. There is of course the complication that Powershell scripting is disabled by default, otherwise using the Powershell scripting facility would be indicated. But e.g. in this case a [cmd.exe] batch file would be more natural than doing its job from C++.
The Windows Script Host shell objects, available from JScript and VBScript, provide functionality for process execution with output grabbing.
There is a little snag in that you then have to poll the output, but I think it's still easier than doing this at the C/C++ API level.
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.