On Thu, Mar 11, 2004 at 01:10:16AM +1100, Matthew Wlazlo wrote:
> Ok let's try that again (hope i read the question right this time!)
> 
> using namespace std;
> using namespace __gnu_cxx;
> 
> ....
> 
> int fd = open("test.txt", O_RDONLY);
> stdio_filebuf<char> in(fd, ios::in, false, 1024);
> istream inf(&in);
> 
> string str;
> while(!inf.eof()) {
>       inf >> str;
>       cout << str << endl;
> }
> 
> close(fd);

Thanks!  I modified that program to use popen,
added the required headers, did a bit of error checking
and stuff and got the following.  Tested under
solaris/gcc3.3.1 and fedora/gcc3.3.2:

Note that I needed to change your if (!feof...).
because I was getting "world" twice.  I guess
that was a bug.  Anyway while(inf << ...) is
more idiomatic imho.  I got the same bug with
fopen as well as popen, fwiw.




#include <iostream>
#include <cstdio>
#include <ext/stdio_filebuf.h>


using namespace std;
using namespace __gnu_cxx;


int
main()
{

        FILE* fp = popen("echo hello, world", "r");
        if (0 == fp) {
                perror("popen");
                return 1;
        }

        stdio_filebuf<char> in(fp, ios::in);
        istream inf(&in);

        string str;
        while(inf >> str)
        {
                cout << str << endl;
        }
        cout << flush;

        if (-1 == fclose(fp)) {
                perror("fclose");
                return 1;
        }
}


-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to