tags:

views:

54

answers:

1

Hi, I’m trying to use the following code in C++ on Mac OS X Snow Leopard to get the output of an external program through a pipe.

FILE * al = popen("program program.cfg", "r");

string data;
char buffer[100];
while (fgets(buffer, 100, al) != NULL)
data.append(buffer);
cout << "«" << data << "»" << endl;

pclose(al);

However, no data gets printed out. I suspect the problem lies in the fact that the external program outputs to wcout and wclog, but I’m not sure how to deal with it. I also tried using a wstring and fgetws, but that didn’t help either.

I read about using boost::iostreams and again had no luck:

FILE * al = popen("program program.cfg", "r");
boost::iostreams::file_descriptor_source alDesc(fileno(al));
boost::iostreams::stream_buffer<boost::iostreams::file_descriptor_source> alStream(alDesc);
istream align(&alStream);

string alignOutput;
while (align) {
    getline(align, alignOutput);
    cout << "«" << alignOutput << "»" << endl;
}
align >> alignOutput;
alStream.close();
alDesc.close();

pclose(al);

Does anyone have a clue as to what the actual problem might be and how to resolve it? In case someone might ask, both the external program and the one reading from the pipe need to use wstring as I’m dealing with data that might be in any language, including Chinese etc.

Thanks in advance for any clues!

A: 

It turned out that I was overwriting a file the external program used for input, so it did not give any output…

Still, it’s nice to have the above snippets at one place, as it wasn’t straightforward to decipher the Boost documentation.

Ventzi Zhechev