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!

Recommended Answers

All 2 Replies

Few checks:-

1. Check if pipe is opened properly.

if(al != NULL)

2. Check if your command is actually outputting something.

Thats all I can see missing in the code.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.