hello, been googling this for a while and can't come up with anything good, so hopefully this won't be so easy to answer that it makes me look lazy.
I'm writing an application for linux (gcc compiler) that calls various system and custom commands from inside my own program, nothing special. I'm using NCurses to make it look a little nicer but the output of the system commands (even with noecho(), odd) becomes unruly and ugly. So I put the commands into pipes, and, using fgets, capture the input line by line and put it where I want, makes it neater. The issue I'm facing is that user-input is required for some of the commands I'm calling, the actual user input is not a problem as the person can just enter their data (passwords usually) and somehow it automatically goes to the pipe's stdin and works, I want to automate the data input. As in, store their information, and, when necessary, put it in automatically, mainly so that it can be run overnight or unsupervised.
Here's the sample scripts I'm using so I can debug my idea before I implement it in my real program:
file 1:
int main()
{
cout << "Press enter to test drive the automated script ";
cin.get();
FILE *file;
char text[255];
std::string temp;
file = popen("[path]/test2", "r");
while(fgets(text, sizeof(text), file)) //gets each line
{
cout << text << endl; //writes each line gotten from the pipe
temp = text; //converts to string to I can use my trim function
trim(temp); //trims the text of any newlines or blanks
if(temp == "7")
{
cout << "hit 7, press 'c' to continue";
cout << endl;
//somehow automatically write 'c' into the stream
}
}
pclose(file);
cout << "Closed pipe";
cout << endl;
return 0;
}
file 2 (being called by file 1):
int main()
{
std::string temp; //this file is pretty self explanatory I think
char input;
for(int x=0;x<10;++x)
{
cout << x << endl;
if(x == 7)
{
do
{
cin >> input;
}
while(input != 'c');
}
}
cout << "Loop finished" << endl;
return 0;
}
I know that if I use "r+" in my popen, I should be able to read and write, but it segmentation faults whenever I add the "+", so I assume somehow it doesn't work for me.
Also note, I would like to keep using pipes as it opens more functionality options for my program, I would be willing to switch to threads, but honestly I don't know much about them. Thanks!
~J