Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

fopen() vs. open()
I know open() returns a file descriptor and fopen() returns a pointer
to FILE.  The question is, when do I use fopen() and when do I use
open()? Could someone give me an example when to use one over the
other?

Report this thread to moderator Post Follow-up to this message
Old Post
Grocery Clerk
10-02-04 01:56 PM


Re: fopen() vs. open()
On 29 Sep 2004 21:33:57 -0700, cdalten@yahoo.com (Grocery Clerk) wrote
in comp.lang.c:

> I know open() returns a file descriptor and fopen() returns a pointer
> to FILE.  The question is, when do I use fopen() and when do I use
> open()? Could someone give me an example when to use one over the
> other?

fopen() and the type FILE * are part of the standard C library,
available on every hosted implementation.

open() and file descriptors are not part of standard C.  They are
non-standard extensions provided by your system, as far as the C
language is concerned.

So if you want to write standard portable C code, use fopen().

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~.../FAQ-acllc.html

Report this thread to moderator Post Follow-up to this message
Old Post
Jack Klein
10-02-04 01:56 PM


Re: fopen() vs. open()
cdalten@yahoo.com (Grocery Clerk) writes:
> I know open() returns a file descriptor and fopen() returns a pointer
> to FILE.  The question is, when do I use fopen() and when do I use
> open()? Could someone give me an example when to use one over the
> other?

Use fopen() when you want to write portable code.  The open() function
is not defined in the C standard.

--
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Keith Thompson
10-02-04 01:56 PM


Re: fopen() vs. open()
On Thu, 2004-09-30 at 00:11 -0500, Jack Klein wrote:
> On 29 Sep 2004 21:33:57 -0700, cdalten@yahoo.com (Grocery Clerk) wrote
> in comp.lang.c:
> 
>
> fopen() and the type FILE * are part of the standard C library,
> available on every hosted implementation.
>
> open() and file descriptors are not part of standard C.  They are
> non-standard extensions provided by your system, as far as the C
> language is concerned.
>
> So if you want to write standard portable C code, use fopen().

It is also worth noting that stdio FILEs, as returned by fopen(), are
(or can be) buffered. Thus, if you want buffered I/O, use fopen().

Fredrik Tolf



Report this thread to moderator Post Follow-up to this message
Old Post
Fredrik Tolf
10-02-04 01:56 PM


Re: fopen() vs. open()
In <1096552161.10848.74.camel@pc7.dolda2000.com> Fredrik Tolf <fredrik@dolda
2000.com> writes:

>On Thu, 2004-09-30 at 00:11 -0500, Jack Klein wrote: 
>
>It is also worth noting that stdio FILEs, as returned by fopen(), are
>(or can be) buffered. Thus, if you want buffered I/O, use fopen().

Buffering is not the issue: files accessed with open() and friends are
buffered, too.

The real issue, apart from portability (open() and friends are actually
*very* portable, even if the standard doesn't guarantee it), is that,
on certain platforms, read() and write() are OS primitives and, therefore,
much more expensive than ordinary library calls.  No big deal when used
with large data chunks, but using read() instead of getc() and write()
instead of putc() may destroy the program's performance.  That's why
the <stdio.h> I/O routines add one *additional* layer of buffering.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
Currently looking for a job in the European Union

Report this thread to moderator Post Follow-up to this message
Old Post
Dan Pop
10-02-04 01:56 PM


Re: fopen() vs. open()
[snips]

On Thu, 30 Sep 2004 15:49:21 +0200, Fredrik Tolf wrote:
 
>
> It is also worth noting that stdio FILEs, as returned by fopen(), are
> (or can be) buffered. Thus, if you want buffered I/O, use fopen().

Not sure how that's relevant to the post above: since read and open aren't
part of the C standard, as far as C is concerned, they may well be
buffered I/O routines.



Report this thread to moderator Post Follow-up to this message
Old Post
Kelsey Bjarnason
10-02-04 01:56 PM


Re: fopen() vs. open()
Grocery Clerk wrote:

> I know open() returns a file descriptor and fopen() returns a pointer
> to FILE.  The question is, when do I use fopen() and when do I use
> open()? Could someone give me an example when to use one over the
> other?

If you want to use standard C, use fopen().  There is no open() in the C
standard libraries.  A newsgroup for your implementation or platform (or
a similar one supporting POSIX) might be a place to ask; this newsgroup
is for C questions.

Report this thread to moderator Post Follow-up to this message
Old Post
Martin Ambuhl
10-02-04 01:56 PM


Re: fopen() vs. open()
On 29 Sep 2004 21:33:57 -0700, cdalten@yahoo.com (Grocery Clerk) wrote
in comp.lang.c:

> I know open() returns a file descriptor and fopen() returns a pointer
> to FILE.  The question is, when do I use fopen() and when do I use
> open()? Could someone give me an example when to use one over the
> other?

fopen() and the type FILE * are part of the standard C library,
available on every hosted implementation.

open() and file descriptors are not part of standard C.  They are
non-standard extensions provided by your system, as far as the C
language is concerned.

So if you want to write standard portable C code, use fopen().

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~.../FAQ-acllc.html

Report this thread to moderator Post Follow-up to this message
Old Post
Jack Klein
10-02-04 08:55 PM


Re: fopen() vs. open()
cdalten@yahoo.com (Grocery Clerk) writes:
> I know open() returns a file descriptor and fopen() returns a pointer
> to FILE.  The question is, when do I use fopen() and when do I use
> open()? Could someone give me an example when to use one over the
> other?

Use fopen() when you want to write portable code.  The open() function
is not defined in the C standard.

--
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Keith Thompson
10-03-04 01:55 AM


Re: fopen() vs. open()
On Thu, 2004-09-30 at 00:11 -0500, Jack Klein wrote:
> On 29 Sep 2004 21:33:57 -0700, cdalten@yahoo.com (Grocery Clerk) wrote
> in comp.lang.c:
> 
>
> fopen() and the type FILE * are part of the standard C library,
> available on every hosted implementation.
>
> open() and file descriptors are not part of standard C.  They are
> non-standard extensions provided by your system, as far as the C
> language is concerned.
>
> So if you want to write standard portable C code, use fopen().

It is also worth noting that stdio FILEs, as returned by fopen(), are
(or can be) buffered. Thus, if you want buffered I/O, use fopen().

Fredrik Tolf



Report this thread to moderator Post Follow-up to this message
Old Post
Fredrik Tolf
10-03-04 08:55 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

C archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 10:27 AM.

 

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.