I am trying to write a program that interfaces with the Stockfish chess engine via the command line. I've looked into using pipes and redirecting cin/cout, but the problem is that Stockfish runs its own shell instead of just giving single lines out output, e.g.:
~$ stockfish 
Stockfish 261014 64 by Tord Romstad, Marco Costalba and Joona Kiiski
> go movetime 5000
[output]
...
> quit
~$
I tried the following code (from here) to read execute commands and read them back, but it never finishes when I try to run Stockfish and print the output:
#include <string>
#include <iostream>
#include <stdio.h>
std::string exec(char* cmd) {
    FILE* pipe = popen(cmd, "r");
    if (!pipe) return "ERROR";
    char buffer[128];
    std::string result = "";
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL)
            result += buffer;
    }
    pclose(pipe);
    return result;
}
int main(void)
{
    std::cout << exec("ls") << std::endl; // this works fine
    std::cout << exec("stockfish") << std::endl; // this never ends
    return 0;
}
My question is, why didn't this work, and how can I write a program that can send and receive text from the Stockfish shell?