Connecting Tech Pros Worldwide Help | Site Map

popen() and writing to returned file pointer

Kamil Grymuza
Guest
 
Posts: n/a
#1: Jul 23 '05
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
Karl Heinz Buchegger
Guest
 
Posts: n/a
#2: Jul 23 '05

re: popen() and writing to returned file pointer


Kamil Grymuza wrote:[color=blue]
>
> 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().[/color]

Why not? What is wrong with fprintf and fflush on Unix such that
it can't be used.
[color=blue]
> I found on
> the net that write() can be used but I don't know excatly how to use it
> with this function.[/color]

I guess you mean fwrite().
Since all you have is a FILE* (from popen()) you need to use the family
of functions which take a FILE* as the file descriptor.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Kamil Grymuza
Guest
 
Posts: n/a
#3: Jul 23 '05

re: popen() and writing to returned file pointer


Karl Heinz Buchegger wrote:
[color=blue]
> Why not? What is wrong with fprintf and fflush on Unix such that
> it can't be used.
>[/color]

fprintf() is a ANSI C function and I would like to use more 'c++'
version :) and possibly not convert string value to const char*.
So is there function (standard c++) that I could use ? Taking string and
file pointer?

[color=blue]
> I guess you mean fwrite().
> Since all you have is a FILE* (from popen()) you need to use the family
> of functions which take a FILE* as the file descriptor.[/color]

Yes I meant fwrite()

Thanks
Alan Johnson
Guest
 
Posts: n/a
#4: Jul 23 '05

re: popen() and writing to returned file pointer


Kamil Grymuza wrote:[color=blue]
> 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[/color]

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 ;
}
Default User
Guest
 
Posts: n/a
#5: Jul 23 '05

re: popen() and writing to returned file pointer




Kamil Grymuza wrote:[color=blue]
> Karl Heinz Buchegger wrote:
>[color=green]
> > Why not? What is wrong with fprintf and fflush on Unix such that
> > it can't be used.
> >[/color]
>
> fprintf() is a ANSI C function[/color]

It is also an ANSI (ISO) standard C++ function.
[color=blue]
> and I would like to use more 'c++'
> version :) and possibly not convert string value to const char*.
> So is there function (standard c++) that I could use ? Taking string and
> file pointer?[/color]

It can't be done portably. Then again, your program is already
platform-specific due the the use of the non-standard popen(). As there
is NO standard solution to your problem, this is not the right
newsgroup for you. You need to immediately ask on a newsgroup
applicable to your implementation. Possibly comp.unix.programmer will
be correct.



Brian

msalters
Guest
 
Posts: n/a
#6: Jul 23 '05

re: popen() and writing to returned file pointer




Alan Johnson schreef:
[color=blue]
> 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) ;[/color]

__gnu_cxx::stdio_filebuf is of course platform-specific, but the
implemented
functionality isn't. It's not too hard to write a streambuf that writes
to a
FILE*. Your std::fstream streambuf might also support it directly.

Regards,
Michiel Salters

Closed Thread


Similar C / C++ bytes