[Expat-discuss] file pointers or descriptors?

Fred L. Drake, Jr. fdrake at acm.org
Sat Apr 26 00:34:13 EDT 2003


Carlos Pereira writes:
 > I am reading both local and remote files,
 > using HTTP and FTP (anonymous) protocols,
 > and then sending the relevant contents to Expat.
 > 
 > For the sake of elegance and maintenance, I want
 > to handle all the three cases using the same
 > routine to read the file contents.

It's not clear what interface you're using with each of the data
sources; perhaps that would help.

 > At the moment I am using file descriptors and is
 > working fine, but I think it would be better to
 > use file pointers instead, which is ansi standard,

Perhaps.  File descriptors are specified by POSIX, so you're on pretty
safe ground there as well.

 > Is this a good ideia, to convert the internet socket 
 > file descriptors to file pointers with fdopen and 
 > then just use the normal stdio.h functions? is there
 > something that I should be worried about?

My own inclination would be to use file descriptors directly.  Since
you're basically just reading chunks of data and passing them to
Expat, you don't benefit from the buffering added by the stdio library
(using FILE *s).  On Unix, using file descriptors is the native
iterface, so you're not going to beat it be adding an extra layer of
wrappers (stdio), and on Windows, both sets of APIs are wrappers
around the Win32 API.  Sockets are a similar story; the native API on
Unix uses file descriptors, and on Win32, everything's a wrapper
around Win32 anyway.

 > According to K&R, BSD and System V Unixes even
 > use different headers (<unistd.h> and sys/file.h) 
 > to declare the open system call...

Yes, but you'll need to deal with these issues on most large projects
that need to run on both.  You can use the autoconf stuff easily
enough for this.


  -Fred

-- 
Fred L. Drake, Jr.  <fdrake at acm.org>
PythonLabs at Zope Corporation



More information about the Expat-discuss mailing list