Code Comments
Programming Forum and web based access to our favorite programming groups.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?
Post Follow-up to this messageOn 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
Post Follow-up to this messagecdalten@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.
Post Follow-up to this messageOn 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
Post Follow-up to this messageIn <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
Post Follow-up to this message[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.
Post Follow-up to this messageGrocery 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.
Post Follow-up to this messageOn 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
Post Follow-up to this messagecdalten@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.
Post Follow-up to this messageOn 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
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.