popen() and writing to returned file pointer

K

Kamil Grymuza

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
 
K

Karl Heinz Buchegger

Kamil said:
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().

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

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.
 
K

Kamil Grymuza

Karl said:
Why not? What is wrong with fprintf and fflush on Unix such that
it can't be used.

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?

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.

Yes I meant fwrite()

Thanks
 
A

Alan Johnson

Kamil said:
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 ;
}
 
D

Default User

Kamil said:
fprintf() is a ANSI C function

It is also an ANSI (ISO) standard 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?

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
 
M

msalters

Alan Johnson schreef:
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) ;

__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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top