Does anyone know of a way to read in terminal output from bash commands into c++? For example, entering “ls” into the terminal returns the file names in the current directory, is there any way to perhaps import these names into a string or char array or something? I have been looking at fork(), exec(), and pipe(). I was attempting to open a pipe communication between child (system commands entered here) and parent (output read in here) but have been thoroughly unsuccessful. Ideas?
Does anyone know of a way to read in terminal output from bash commands
Share
Thanks Kerrek SB, I had seen people using popen() in various ways but wasn’t sure if that was the route to pursue or not. With the help of the documentation and putting together parts of how other people were using it, I came up with this. This does what the question I wrote asked about specifically: importing the terminal response from the command “ls” into a vector of file names. However, each file name contains a newline character that I implicitly remove pushing back all but the last element of the string file into the vector.
I am trying to adapt the wikipedia RAII example to fit my needs as per the recommendations of Mankarse, and this is what I have so far. Does it look like I am on the right track? Any obvious mistakes or misunderstandings I have on this?
Then call it with this: