By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,082 Members | 2,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,082 IT Pros & Developers. It's quick & easy.

open,fopen differences

P: n/a
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"
/"fread" / "fwrite" are normal library functions.

May be internally must be calling "fopen" /"fread" / "fwrite"
functions.

But what difference does it makes from an application programmer
perspective?Can any one please explain me about this ?

Thank you.

Cheers,
Blue :).
--
Copyright 2004, all wrongs reversed. Peter Seebach / se***@plethora.net
http://www.seebs.net/log/ - YA blog. http://www.seebs.net/ - homepage.
C/Unix wizard, pro-commerce radical, spam fighter. Boycott Spamazon!
Consulting, computers, web hosting, and shell access: http://www.plethora.net/
Nov 14 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Blue wrote:
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"
/"fread" / "fwrite" are normal library functions.

May be internally must be calling "fopen" /"fread" / "fwrite"
functions.

But what difference does it makes from an application programmer
perspective?Can any one please explain me about this ?

Thank you.

Cheers,
Blue :).


From a C perspective, the difference is simple: fopen(), fread(),
fwrite(), etc. exist, the rest do not.

If you want to write portable code, use fopen(), etc. [1]

HTH,
--ag

[1] Of course, if you *need* to use whatever platform specific lower
level primitives exist on your platform, by all means do so -- but
further questions about them will be off topic here.

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #2

P: n/a
Artie Gold <ar*******@austin.rr.com> wrote in news:3amqrcF6e6uaqU1
@individual.net:
Blue wrote:
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"
/"fread" / "fwrite" are normal library functions.

May be internally must be calling "fopen" /"fread" / "fwrite"
functions.

But what difference does it makes from an application programmer
perspective?Can any one please explain me about this ?

Thank you.

Cheers,
Blue :).


From a C perspective, the difference is simple: fopen(), fread(),
fwrite(), etc. exist, the rest do not.

If you want to write portable code, use fopen(), etc. [1]

HTH,
--ag

[1] Of course, if you *need* to use whatever platform specific lower
level primitives exist on your platform, by all means do so -- but
further questions about them will be off topic here.

fopen/fread/fwrite have buffer to fasten I/O process, but open/read/write
dosen't

Nov 14 '05 #3

P: n/a
On Sun, 27 Mar 2005 06:12:15 +0000 (UTC), bigben <bi****@galaway.com>
wrote in comp.lang.c:
Artie Gold <ar*******@austin.rr.com> wrote in news:3amqrcF6e6uaqU1
@individual.net:
Blue wrote:
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"
/"fread" / "fwrite" are normal library functions.

May be internally must be calling "fopen" /"fread" / "fwrite"
functions.

But what difference does it makes from an application programmer
perspective?Can any one please explain me about this ?

Thank you.

Cheers,
Blue :).


From a C perspective, the difference is simple: fopen(), fread(),
fwrite(), etc. exist, the rest do not.

If you want to write portable code, use fopen(), etc. [1]

HTH,
--ag

[1] Of course, if you *need* to use whatever platform specific lower
level primitives exist on your platform, by all means do so -- but
further questions about them will be off topic here.

fopen/fread/fwrite have buffer to fasten I/O process, but open/read/write
dosen't


Where in the ISO C standard does it state what "open", "read" and
"write" do? Nowhere, because such functions are not even defined.
They do whatever the implementation says they do, and whatever that is
it is not a C matter.

What if the "open" function on some platform popped out the tray on
the CD drive? What in the C standard prohibits it?

--
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 #4

P: n/a
in the au jus.

Bisque l?Enfant

Honor the memory of Grandma with this dish by utilizing her good
silver soup tureen and her great grandchildren (crawfish, crab or
lobster will work just as well, however this dish is classically
made with crawfish).

Stuffed infant heads, stuffed crawfish heads, stuffed crab or lobster shells;
make patties if shell or head is not available
(such as with packaged crawfish, crab, or headless baby).
Flour
oil
onions
bell peppers
garlic salt, pepper, etc.
3 cups chicken stock
2 sticks butter
3 tablespoons oil

First stuff the heads, or make the patties (see index)
then fry or bake.
Set aside to drain on paper towels.
Make a roux with butter, oil and flour,
brown vegetables in the roux, then add chicken stock and
allow to simmer for 20 minutes.
Add the patties or stuffed heads, and some loose crawfish,
lobster, long piglet, or what have you.
Cook on low for 15 minutes, then allow it to set for at least
15 minutes more.
Serve over steamed rice; this dish is very impressive!

Stuffed Cabbage Rolls

Babies really can be found under a cabbage leaf -
or one can arrange for ground beef to be found there instead.

8 large cabbage leaves
1 lb. lean ground newborn human filets, or ground chuck
Onions
peppers
celery
garlic
soy sauce
salt pepper, etc
Olive oil
breadcrumbs
Tomato Gravy (see index)

Boil the cabbage leaves for 2 minut
Nov 14 '05 #5

P: n/a
>Can any one please let me explain me the diffrences between "open"/
"fopen" or "read"/"fread" or "write/fwrite".
fopen(), fread(), and fwrite() are present in ANSI C. open(),
read(), and write() are not, but they are present in POSIX.
I know that "open" /"read" / "write" are system calls and "fopen"
/"fread" / "fwrite" are normal library functions.
On a particular implementation, this may be correct. In general,
the line between "system calls" and "library functions" is fuzzy
at best and meaningless at worst.
May be internally must be calling "fopen" /"fread" / "fwrite"
functions.
Parse sense no.
But what difference does it makes from an application programmer
perspective?Can any one please explain me about this ?


ANSI C is more portable than POSIX. DO NOT try to mix the two sets.
The representation of an "open file handle" or whatever you call
it is very different between open() and fopen().

Gordon L. Burditt
--
Copyright 2004, all wrongs reversed. Peter Seebach / se***@plethora.net
http://www.seebs.net/log/ - YA blog. http://www.seebs.net/ - homepage.
C/Unix wizard, pro-commerce radical, spam fighter. Boycott Spamazon!
Consulting, computers, web hosting, and shell access: http://www.plethora.net/
Nov 14 '05 #6

P: n/a
Blue wrote:
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"
/"fread" / "fwrite" are normal library functions.
fopen/fclose/fread/fwrite are C standard library functions.

open/close/read/write _may_ be functions provided by your operating
systems or come from somewhere else -- or they might not be there
at all; they are not standard C.

If you want your programs to be portable or as portable as possible,
restrict yourself to the standard library (whenever possible).

May be internally must be calling "fopen" /"fread" / "fwrite"
functions.
It is possible that internally fopen() & Co. use open() and
friends if available.

But what difference does it makes from an application programmer
perspective?Can any one please explain me about this ?


If you want file access functions and the standard library functions
are sufficient for your purposes, then use them. Only if the other
functions provide something you cannot get using the standard library,
use the other functions.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
--
Copyright 2004, all wrongs reversed. Peter Seebach / se***@plethora.net
http://www.seebs.net/log/ - YA blog. http://www.seebs.net/ - homepage.
C/Unix wizard, pro-commerce radical, spam fighter. Boycott Spamazon!
Consulting, computers, web hosting, and shell access: http://www.plethora.net/
Nov 14 '05 #7

P: n/a
Blue wrote:
But what difference does it makes from an application programmer
perspective?Can any one please explain me about this ?


open() is not standard and might not be implemented in
some standard-conforming hosted environments, whereas
fopen() is standard and is implemented in all standard-
conforming hosted environments. Therefore for
portability, use the standard functions, not (emulated)
Unix system service calls.

There are other functional differences, the most
notable being that the stdio stream uses a buffer in
"user space", whereas the direct system-call I/O does
not. (There might or might not be kernel-space
buffers in both cases.) This can matter if record
length is important; for example, if you want to write
a magnetic tape record of length 80, write(fd,buf,80)
will do that, whereas fwrite(buf,80,1,fp) will likely
just put the data in a user-space buffer, with the
actual write occurring eventually when the buffer is
full - which will result in something like a 512-byte
magtape record. You could perhaps work around that by
issuing a fflush(fp) after the fwrite(), but it's a
kludgy way to accomplish what write() does directly.
--
Copyright 2004, all wrongs reversed. Peter Seebach / se***@plethora.net
http://www.seebs.net/log/ - YA blog. http://www.seebs.net/ - homepage.
C/Unix wizard, pro-commerce radical, spam fighter. Boycott Spamazon!
Consulting, computers, web hosting, and shell access: http://www.plethora.net/
Nov 14 '05 #8

P: n/a
Blue wrote:

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"
/"fread" / "fwrite" are normal library functions.


No you don't. You know that fopen, fread, fwrite, fclose are
available because the standard says they must be. You KNOW
absolutely nothing about open, read, write, because the standard
says nothing about them. They may be present on some systems, in
some form or other, but that has nothing to do with the C
language. You would be well advised to ignore them unless forced
into building some non-portable non-standard peculiar software.

Because open, read, write etc. are non-standard, they are off-topic
here. They may well be topical on a newsgroup dedicated to your
particular system.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
--
Copyright 2004, all wrongs reversed. Peter Seebach / se***@plethora.net
http://www.seebs.net/log/ - YA blog. http://www.seebs.net/ - homepage.
C/Unix wizard, pro-commerce radical, spam fighter. Boycott Spamazon!
Consulting, computers, web hosting, and shell access: http://www.plethora.net/
Nov 14 '05 #9

P: n/a
Blue wrote:
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"
/"fread" / "fwrite" are normal library functions.

May be internally must be calling "fopen" /"fread" / "fwrite"
functions.

But what difference does it makes from an application programmer
perspective?Can any one please explain me about this ?


(a) fopen() et al are standardized in the C standard; open() et al are
standardized by POSIX. There are more standard C platforms than POSIX
platforms, so fopen() et al are more portable (if used carefully).

(b) fopen() et al deal with FILE * whereas open() et al use an integer.

(c) typically, open() in some variation is used by fopen() - the reason
for 'some variation' being that you are permitted (in standard C) to
write your own function called open() and the fopen() is not allowed to
call it instead of the system function.

(d) There are options that can be specified to open() that cannot be
specified to fopen(). If you need specialized control over the device,
you probably need to use open(). That said, you are immediately
limiting the portability of your application - because you require those
special semantics.

(e) Guideline: use fopen() et al until you find you can't do something.
Then work out how you can do it after all. Only if you really, really,
really can't do it, then drop into using open() etc.

--
Jonathan Leffler #include <disclaimer.h>
Email: jl******@earthlink.net, jl******@us.ibm.com
Guardian of DBD::Informix v2005.01 -- http://dbi.perl.org/
--
Copyright 2004, all wrongs reversed. Peter Seebach / se***@plethora.net
http://www.seebs.net/log/ - YA blog. http://www.seebs.net/ - homepage.
C/Unix wizard, pro-commerce radical, spam fighter. Boycott Spamazon!
Consulting, computers, web hosting, and shell access: http://www.plethora.net/
Nov 14 '05 #10

P: n/a
Blue wrote:
Hi ,

Can any one please let me explain me the diffrences between "open"/
"fopen" or "read"/"fread" or "write/fwrite".
open, ... are part of POSIX. fopen, ... are part of the standard
libarary.

I know that "open" /"read" / "write" are system calls and "fopen"
/"fread" / "fwrite" are normal library functions.

May be internally must be calling "fopen" /"fread" / "fwrite"
functions.
Not sure what your saying is totally correct. This is discussed in
"The Standard C Library" by Plauger. It is probably worth your time to
read the section on stdio if you haven't already.

But what difference does it makes from an application programmer
perspective?Can any one please explain me about this ?


Generally you should use fopen and friends because they are part of the
standard library. Also for most situations they will be faster. If
you are writing large amounts of data (multiple pages at a time) you
may want to consider using open and friends (but fread and fwrite do
fine in this case too).

-Charlie
--
Copyright 2004, all wrongs reversed. Peter Seebach / se***@plethora.net
http://www.seebs.net/log/ - YA blog. http://www.seebs.net/ - homepage.
C/Unix wizard, pro-commerce radical, spam fighter. Boycott Spamazon!
Consulting, computers, web hosting, and shell access: http://www.plethora.net/
Nov 14 '05 #11

P: n/a
In comp.lang.c.moderated Blue <ub**********@gmail.com> wrote:
Can any one please let me explain me the diffrences between "open"/
"fopen" or "read"/"fread" or "write/fwrite".
The first element in each pair is a system-specific, non-portable
thing, the other is a properly standardized function of well-defined
behaviour.
But what difference does it makes from an application programmer
perspective?


The 'f' varieties allow you to write portable program's. What a
function called 'open()' might do on some random computer platform is
anyone's guess.

--
Hans-Bernhard Broeker (br*****@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
--
Copyright 2004, all wrongs reversed. Peter Seebach / se***@plethora.net
http://www.seebs.net/log/ - YA blog. http://www.seebs.net/ - homepage.
C/Unix wizard, pro-commerce radical, spam fighter. Boycott Spamazon!
Consulting, computers, web hosting, and shell access: http://www.plethora.net/
Nov 14 '05 #12

P: n/a
On Sun, 27 Mar 2005 03:10:33 +0000, Blue wrote:
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"
/"fread" / "fwrite" are normal library functions.

May be internally must be calling "fopen" /"fread" / "fwrite"
functions.

But what difference does it makes from an application programmer
perspective?Can any one please explain me about this ?


Well, you've pretty much covered it. open/read/close are POSIX system
calls, and are not portable to non POSIX systems, unlike f* ones. On unix
systems, open/read/write will be used internally, since there are the
operating system primitives for accessing files. Further, f* functions
provide buffering and formatted I/O, which the primitive calls do not do.

If you want portable C, use f* functions. If you need to access unix
functionality, like ioctl, mmap, tty mangling, etc, then you probably want
to use the unix primitives instead.

Also, some C libraries (I don'e believe this is standard, but I don't know
for sure), such as the GNU C one allow you to create custom FILE streams,
so you can read from things not natively represented by files on the
system (for instance, blocks of memory), which is a very, nice flexible
way of doing things.

-Ed

--
(You can't go wrong with psycho-rats.) (er258)(@)(eng.cam)(.ac.uk)

/d{def}def/f{/Times findfont s scalefont setfont}d/s{10}d/r{roll}d f 5/m
{moveto}d -1 r 230 350 m 0 1 179{1 index show 88 rotate 4 mul 0 rmoveto}
for /s 15 d f pop 240 420 m 0 1 3 { 4 2 1 r sub -1 r show } for showpage
--
Copyright 2004, all wrongs reversed. Peter Seebach / se***@plethora.net
http://www.seebs.net/log/ - YA blog. http://www.seebs.net/ - homepage.
C/Unix wizard, pro-commerce radical, spam fighter. Boycott Spamazon!
Consulting, computers, web hosting, and shell access: http://www.plethora.net/
Nov 14 '05 #13

P: n/a
> May be internally must be calling "fopen" /"fread" / "fwrite"
functions.
Other way around -- fopen calls open, fread calls read, etc.
But what difference does it makes from an application programmer
perspective?Can any one please explain me about this ?


Application programmers normally don't need open and read, etc. Using
open and read gets you into system development issues, like restarting
system calls, buffered input/output, and all of that fun. The "f" class
functions take all of the dirty work away, while the other ones deal
directly with the operating system and all its ugliness.

You also have many more functions available with the "f" functions,
while with the open/read/write, that's pretty much all you get.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
--
Copyright 2004, all wrongs reversed. Peter Seebach / se***@plethora.net
http://www.seebs.net/log/ - YA blog. http://www.seebs.net/ - homepage.
C/Unix wizard, pro-commerce radical, spam fighter. Boycott Spamazon!
Consulting, computers, web hosting, and shell access: http://www.plethora.net/
Nov 14 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.