My questions are:
When should you use open/write and when should you use fopen/fputs?
What about fwrite?
Is one faster than the other or is it really operator preference?
Is the open/write to deal with pipes?
thanx,
js
>I have done most of my C work (what there is of it) in non-UNIX
>environments and have recently encountered the open(/get?)/write/close
>functions. I am used to using fopen/fgets/fputs/fclose. Also, we are
>really only dealing with files.
Then, you can safely continue to use the stdio functions. If you perform
your I/O in large chunks of bytes, read/write might be more efficient,
otherwise the stdio functions are the performance winners.
>My questions are:
>When should you use open/write and when should you use fopen/fputs?
Use open/write only when, for one reason or another, you cannot use
their stdio counterparts.
>What about fwrite?
Ditto.
>Is one faster than the other or is it really operator preference?
Already answered.
>Is the open/write to deal with pipes?
open/write are usually needed when doing Unix system programming, as
opposed to normal application programming. The Unix system calls
need file descriptors, while the FILE pointers are an abstraction
provided by the stdio library.
Dan
--
Dan Pop
CERN, IT Division
Email: Dan...@cern.ch
Mail: CERN - PPE, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
Never use the "open/write/etc." functions. They are not part of standard
C and their implementation is compiler specific. They do not offer
any real advantages over the "fopen(), fclose(), f...()" at the very
high priceof depending on the compiler.
> What about fwrite?
"fread()" and "fwrite()" are well suited to read a specific amount of
binary data from a file.
> Is one faster than the other or is it really operator preference?
Yes, within the "f...()" family of functions there are, depending on the
what they do: reading more data in one go is conceptionally faster, but
having to do some checks on the costs performance. Please read the
documentation of the functions.
> Is the open/write to deal with pipes?
Standard C does not support pipes. They are an OS specific feature.
You can ask the experts for your OS in the corresponding programmers
newsgroup. For Unix that would be:
news:comp.unix.programmer
Stephan
(self appointed member of the campaign against grumpiness in c.l.c)
There's more than one philosophy that can be used here, but I use fopen and
its friends when dealing with text files and the open family for binary files.
Of course, it's possible to use either family for either type of file; that's
just what makes sense for me. I use the dreaded fprintf for output to text
files a lot (for convenience) so I use the fopen family for that.
>What about fwrite?
>Is one faster than the other or is it really operator preference?
It's implementation-dependent, but usually the fopen family has more overhead.
In the original UNIX implementation the fopen family was a buffered wrapper
around the open family, which were the native system calls, IIRC (it's been a
while since I did any serious UNIX system programming.) To emphasize the
point about implementation dependence, one compiler I used for a while
actually implemented open by calling fopen, so there's no easy answer.
>Is the open/write to deal with pipes?
No, not really, although it works with them, too. It was the original set of
UNIX system calls for I/O.
-Will Flor w...@geocities.com
>It's implementation-dependent, but usually the fopen family has more overhead.
One of the great advantages of the fopen() family is that they can employ
user level buffering that *reduces* overhead, especially under Unix.
--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------