From: Paul Brannan Date: 2001-07-26T03:39:46+09:00 Subject: [ruby-talk:18538] Re: Opening a FILE* in a C extension One possibility is to use rb_file_open (which itself calls fopen) to open the file, then use GetReadFile and/or GetWriteFile and/or GetOpenFile to get the FILE* pointer. e.g. (untested): #include VALUE io = rb_file_open("foo", "r+); OpenFile *open_file; FILE *rfp, *wfp; GetOpenFile(io, open_file); rfp = GetReadFile(open_file); wfp = GetWriteFile(open_file); Normally, rfp and wfp will be the same, but this is not true in the case of a pipe. Be forewarned that some of the above function calls can raise exceptions (specifically rb_file_open and GetOpenFile) so be careful when using them. Paul On Thu, 26 Jul 2001, Brett W. Denner wrote: > In a C extension, I need to read data from a file using fread. I also want > to make sure that the file is closed after the read (even if an exception is > raised, for example). What is the best way to do this in Ruby? Should I > just use fopen and register the FILE* with Ruby's garbage collection? Do I > need to manually fclose all my FILE* pointers? Or is there a better way to > make sure my files get closed automatically? > > Thanks, > > Brett > >