I have a program in Linux C++ in VSCode that I am trying to debug. I am using Boost process::ipstream to send a command line command like "ip -a", then try to use std::getLine() to read the iostream and process the response which is a multi-line listing of all network interface information. I have tried quite a few different methods but no matter what I do as soon as I send out the command I see the response in the VSCode Terminal and my coded reader logic gets nothing. I have read up on VSCode terminal settings to see about not having it parse the responses but have found no settings that work in VSCode's settings.json file.
I feel like the core issue is VSCode stealing the responses in terminal so I cannot get them in my code. Has anybody seen anything like this and how can I disable Terminal from parsing responses to the CLI commands I am sending?
For the record, what I am really trying to do is run the speedtest cli application form my C++ code and parse the responses there, but I thought I'd try something simple first so was just trying to be able to parse results from the "ip -a" command.
I know, code is good. This is the latest thing I am trying to do (in desperation I tried something from ChatGPT):
io_service io;
ip::tcp::resolver resolver(io);
boost::process::ipstream output;
// Set up the command
std::string command = "ip -a";
// Set a timeout (in seconds) for the operation
int timeout = 5;
// Start the child process to run the ping command
boost::process::child c(command, boost::process::std_out > output, io);
// As soon as I step over the above line I see the response in VSCode Terminal view
// Wait for the process to exit or timeout
if (c.wait_for(std::chrono::seconds(timeout)) == 0) {
// Process completed within the timeout
// Read and parse the output of the ping command
std::string line;
while (std::getline(output, line)) {
// Parse and process the ping output here
std::cout << "GLH: " << line << std::endl;
}
} else {
// Process didn't complete within the timeout
// I hit this timeout every time
std::cout << "Timeout occurred." << std::endl;
}
Thanks
I can not reproduce the issue. First of all,
ip -aseems to be an invalid command. It is possible that the output appears in the terminal because it is error information, going tostderrinstead?Here's my simplified approach:
Live On Compiler Explorer
It prints:
I'd consider parsing immediately, e.g.:
Which prints: