Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

converting perl file handle into C FILE *

by kamesh3183 (Beadle)
on Aug 31, 2005 at 18:09 UTC ( [id://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

Replies are listed 'Best First'.
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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-04-18 13:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found