Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

converting perl file handle into C FILE *

by kamesh3183 (Beadle)
on Aug 31, 2005 at 18:09 UTC ( #488179=perlquestion: print w/ replies, xml ) Need Help??
kamesh3183 has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,
I am writing a wrapper for a C function:
int fun1(FILE *, char *);

Can somebody tell me how to convert the Perl filehandle into a C FILE * ?
presently I am using code like this:
arg1 = IoIFP(sv_2io(ST(1));
but the IoIFP is returning struct _PerlIO * type. Can anybody tell me how can I convert into FILE *?
Thanks
Kamesh

Comment on converting perl file handle into C FILE *
Download Code
Re: converting perl file handle into C FILE *
by merlyn (Sage) on Aug 31, 2005 at 18:12 UTC
    If you're using PerlIO instead of STDIO, you probably won't be able to do what you want, because the buffer layout is likely incompatible.

    If all you want is a FILE * that is open on the same filedescriptor (which means that buffered I/O will almost certainly be broken), you can get the fileno from Perl, then create a FILE * on that using fdopen(3). That's probably not very safe though, because Perl will eventually close the handle leaving your FILE * pointing at a dead handle.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: converting perl file handle into C FILE *
by Roger (Parson) on Aug 31, 2005 at 18:14 UTC
    You can use the XS::Typemap module, and use the convertion function T_STDIO to convert perl I/O structure to FILE * structure to pass to your XS.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://488179]
Approved by Roger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2015-07-03 11:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (51 votes), past polls