Unix Programming - FILE* and fd

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2005 > FILE* and fd





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author FILE* and fd
vineeth

2005-10-24, 3:48 pm

hi all,
Is there any advantage in using FILE* related api calls instead of
using the file descriptor and related calls like read()?
pls share your ideas.

Gordon Burditt

2005-10-24, 3:48 pm

>Is there any advantage in using FILE* related api calls instead of
>using the file descriptor and related calls like read()?
>pls share your ideas.


The use of, say, getc() and putc() calls or other
single-character-at-a-time I/O operations using stdio can be
significantly faster than single-character read() or write() calls.

If you're doing I/O in large blocks anyway, reading and writing in
64k read() or write() calls might be faster than stdio calls (which
might use a buffer like 512 bytes or 4k or 16k).

This can interact with the sector interleave of floppies and hard
disks to the point that writing a whole floppy as a buffered device
can take 1 minute with stdio and over an hour with
single-character-at-a-time write() calls.

Gordon L. Burditt
Keith Thompson

2005-10-24, 3:48 pm

"vineeth" <nvineeth@gmail.com> writes:
> Is there any advantage in using FILE* related api calls instead of
> using the file descriptor and related calls like read()?
> pls share your ideas.


The FILE* calls are defined by the C language standard. The calls
using file descriptors are not; they're defined by the POSIX standard.
Code using Unix-style file descriptors is less portable than code that
uses only the standard FILE* interface.

(This may not be relevant if you don't care about portability to
non-Unix system.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
50295@web.de

2005-10-28, 4:53 pm


Keith Thompson wrote:
> "vineeth" <nvineeth@gmail.com> writes:
>
> The FILE* calls are defined by the C language standard. The calls
> using file descriptors are not; they're defined by the POSIX standard.
> Code using Unix-style file descriptors is less portable than code that
> uses only the standard FILE* interface.
>
> (This may not be relevant if you don't care about portability to
> non-Unix system.)


Just a note, the file decriptor is a member of the FILE structure. i.e:


typedef struct {
unsigned char *curp; /* Current active pointer (mandatory) */
unsigned char *buffer; /* Data transfer buffer (mandatory) */
int level; /* fill/empty level of buffer */
int bsize; /* Buffer size (mandatory) */
unsigned short istemp; /* Temporary file indicator */
unsigned short flags; /* File status flags (mandatory) */
wchar_t hold; /* ungetc char if no buffer exists */
char fd; /* FILE DESCRIPTOR (mandatory) */
unsigned char token; /* Used for validity checking */
} FILE; /* This is the FILE type

Even the MSDN has its versions of read(2) and write(2) -- using file
descriptors. See:
_write() : http://tinyurl.com/ddon8
_read() : http://tinyurl.com/9ju9e

Lew Pitcher

2005-10-28, 4:53 pm

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

50295@web.de wrote:
> Keith Thompson wrote:
>
>
>
> Just a note, the file decriptor is a member of the FILE structure. i.e:


on the platform you got that from.

I can promise you that the FILE structure is not the same on all platforms

> Even the MSDN has its versions of read(2) and write(2) -- using file
> descriptors.


/But/, systems like Z/OS (OS/390, MVS, etc.) /do not/ have file descriptors.

- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDYRxuagVFX4UWr64RAiB3AJ44hHf8vMRM
0Hkg7f1KgWLaXRUpcACfcEdQ
IKh4JFs2vuUo0jH4ZNaVkgs=
=iCat
-----END PGP SIGNATURE-----
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com