I'm using popen()
to open a third party child process (called gmsh). This child process prints its output into my console despite me not writing code for it to do so (std::cout inside the 'while' loop doesn't make any difference):
#include <iostream>
#include <array>
int main()
{
std::array<char, 128> buffer;
std::string cmd = "gmsh ex0.geo -2";
FILE* pipe = popen(cmd.c_str(), "r");
if (!pipe)
{
std::cerr << "Couldn't start gmsh" << std::endl;
return -1;
}
while (fgets(buffer.data(), 128, pipe) != NULL) {
//std::cout << buffer.data();
}
pclose(pipe);
return 0;
}
It is very important that I stop it from doing so because it is used in multiple threads and the calculations are heavy so speed is important and the printing slows the program immensely. And since it is multithreaded the child processes print simultaneously so it's unreadable and useless anyway.
The third party program I'm using is called gmsh. I'm on windows 10.
I tried to understand why it prints so I made a dummy executable with one std::cout inside and as expected when opening it as a child program it does not print unless I print the buffer. Interestingly though, if the dummy executable has one std::cerr in it. it will behave like the third party program I'm trying to use. ie print it regardless of whether I want to or not.
Thanks.