472,993 Members | 2,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 software developers and data experts.

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?
Nov 14 '05 #1
10 32260
On 29 Sep 2004 21:33:57 -0700, cd*****@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/~a...FAQ-acllc.html
Nov 14 '05 #2
cd*****@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) ks***@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.
Nov 14 '05 #3
On Thu, 2004-09-30 at 00:11 -0500, Jack Klein wrote:
On 29 Sep 2004 21:33:57 -0700, cd*****@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().


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
Nov 14 '05 #4
In <10***********************@pc7.dolda2000.com> Fredrik Tolf <fr*****@dolda2000.com> writes:
On Thu, 2004-09-30 at 00:11 -0500, Jack Klein wrote:
On 29 Sep 2004 21:33:57 -0700, cd*****@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().


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: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #5
[snips]

On Thu, 30 Sep 2004 15:49:21 +0200, Fredrik Tolf wrote:
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().


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.
Nov 14 '05 #6
On Thu, 2004-09-30 at 15:20 +0000, Dan Pop wrote:
In <10***********************@pc7.dolda2000.com> Fredrik Tolf <fr*****@dolda2000.com> writes:
On Thu, 2004-09-30 at 00:11 -0500, Jack Klein wrote:
On 29 Sep 2004 21:33:57 -0700, cd*****@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().


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.


I know that buffering wasn't the issue -- I just thought I'd add it. I
realize that maybe I should have marked the message OT, though.

<OT>
Files opened with open() need not even necessarily be buffered,
regardless of whether they are syscalls -- It depends on the type of
file, the operating system and many other factors (it could be opened
with O_SYNC, on a filesystem mounted synchronously, be a terminal are
other device, or whatever).

Files opened with fopen(), however, are guaranteed to be buffered. Even
if they aren't buffered from the start, they can always be made
buffered.
</OT>

Fredrik Tolf
Nov 14 '05 #7
In <10***********************@pc7.dolda2000.com> Fredrik Tolf <fr*****@dolda2000.com> writes:
On Thu, 2004-09-30 at 15:20 +0000, Dan Pop wrote:
In <10***********************@pc7.dolda2000.com> Fredrik Tolf <fr*****@dolda2000.com> writes:
>On Thu, 2004-09-30 at 00:11 -0500, Jack Klein wrote:
>> On 29 Sep 2004 21:33:57 -0700, cd*****@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().
>
>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.


I know that buffering wasn't the issue -- I just thought I'd add it. I
realize that maybe I should have marked the message OT, though.

<OT>
Files opened with open() need not even necessarily be buffered,
regardless of whether they are syscalls -- It depends on the type of
file, the operating system and many other factors (it could be opened
with O_SYNC, on a filesystem mounted synchronously, be a terminal are
other device, or whatever).

Files opened with fopen(), however, are guaranteed to be buffered. Even
if they aren't buffered from the start, they can always be made
buffered.
</OT>


You're mixing up two layers of buffering: the one performed by the OS and
the one performed by <stdio.h> routines. Both count as buffering and both
can be enabled or disabled by the application, so I fail to see your
point.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #8
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.
Nov 14 '05 #9
On Thu, 2004-09-30 at 19:08 +0000, Dan Pop wrote:
In <10***********************@pc7.dolda2000.com> Fredrik Tolf <fr*****@dolda2000.com> writes:
On Thu, 2004-09-30 at 15:20 +0000, Dan Pop wrote:
In <10***********************@pc7.dolda2000.com> Fredrik Tolf <fr*****@dolda2000.com> writes:

>On Thu, 2004-09-30 at 00:11 -0500, Jack Klein wrote:
>> On 29 Sep 2004 21:33:57 -0700, cd*****@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().
>
>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.


I know that buffering wasn't the issue -- I just thought I'd add it. I
realize that maybe I should have marked the message OT, though.

<OT>
Files opened with open() need not even necessarily be buffered,
regardless of whether they are syscalls -- It depends on the type of
file, the operating system and many other factors (it could be opened
with O_SYNC, on a filesystem mounted synchronously, be a terminal are
other device, or whatever).

Files opened with fopen(), however, are guaranteed to be buffered. Even
if they aren't buffered from the start, they can always be made
buffered.
</OT>


You're mixing up two layers of buffering: the one performed by the OS and
the one performed by <stdio.h> routines. Both count as buffering and both
can be enabled or disabled by the application, so I fail to see your
point.


My point is that not all file descriptors can be buffered. Take terminal
or device I/O files, for example. That's my main point -- not all file
descriptors can be buffered. Stdio FILEs, however, can _always_ be
buffered.
Nov 14 '05 #10
In <10***********************@pc7.dolda2000.com> Fredrik Tolf <fr*****@dolda2000.com> writes:
My point is that not all file descriptors can be buffered.
Then, your point is wrong.
Take terminal or device I/O files, for example.
They are buffered on my system, by default. E.g. removing the <stdio.h>
buffering on stdin (when connected to the terminal) has no effect, because
/dev/tty itself is line buffered, by default, at the OS level. I need to
change the buffering at this layer (using the <termios.h> interface)
if I want to get characters as soon as they are available, without
waiting for the user to press the Return/Enter key.
That's my main point -- not all file descriptors can be buffered.
Again, your point is wrong.
Stdio FILEs, however, can _always_ be buffered.


At a *different* layer, but this point seems to be too subtle for you.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Corne' Cornelius | last post by:
Hi, When you open a file for writing/appending with open() or fopen(), and you have multiple applications that might want to write to the same file at the same time, could that cause weirdness...
2
by: Thomas Baruchel | last post by:
Hi, wondering about: func1: setjmp() ; func2(); func2: {FILE *f; f = fopen(); func3(); fclose(f)} func3 : if() longjmp; else return; Note that FILE *fis a local variable in func2.
13
by: Blue | last post by:
Hi , Can any one please let me explain me the diffrences between "open"/ "fopen" or "read"/"fread" or "write/fwrite". I know that "open" /"read" / "write" are system calls and "fopen"...
4
by: Frank | last post by:
Could someone tell me how to open a file at run time that I didn't know the name of at compile time? I know how to open a file at compile time when I know what the name is going to be. FILE...
10
by: pjlsr | last post by:
It's close to twenty years since I used the C language and at that time I was doing only floating point computational work, nothing with strings or reading files. I tried to use fopen in the...
27
by: Jack | last post by:
What is the difference betweeb the two functions? Thanks.
4
by: Claire DURAND | last post by:
Hi, I try to open a distant (not local) XML file in PHP to read an RSS feed. I can open an HTML page and read it with the file() function. But as soon as I try with a RSS feed instead of...
10
by: Julia | last post by:
Hi, there, I am trying to append a binary file by using: FILE *strean; stream = fopen( "c:\temp.dat", "ba+" )); Is there a way that I can check if the file exists or not before fopen,...
25
by: subramanian100in | last post by:
Consider the following program: #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { if (argc != 2) { printf("Usage: <program-name<text-file>\n");
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.