Kamil Grymuza wrote:
Hi
What is the best way to write to file pointer returned by popen().
I'm writing on Unix / c++ so I don't want to use fprintf() and then
fflush(). I found on
the net that write() can be used but I don't know excatly how to use it
with this function.
Thanks very much
Kamil Grymuza
This is, of course, completely non-standard, but if you are using a
suitable platform you can construct a stream object from the FILE *.
The following program demostrates the correct (I think) way to do this.
Alan
#include <ext/stdio_filebuf.h>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <iostream>
using namespace std ;
int main()
{
FILE *fp = popen("ls -l", "r") ;
__gnu_cxx::stdio_filebuf<char> fb(fp, ios::in) ;
istream f(&fb) ;
for (char c = f.get(); !f.eof(); c = f.get())
cout << c ;
pclose(fp) ;
return 0 ;
}