|
|
open() and fopen()
What is the difference betweeb the two functions? Thanks.
|
|
0
|
|
|
|
Reply
|
junw2000 (221)
|
7/14/2006 4:39:52 PM |
|
Jack wrote:
> What is the difference betweeb the two functions? Thanks.
I don't know what answer would help you more than just reading the
descriptions of each function. The general idea is that 'fopen' aims to
be a portable C function to open a file whereas 'open' is a portable
POSIX function to open a file. On most POSIX systems, the C stdio
library (and thus 'fopen') is implemented on top of 'open'.
They just come from two different standards.
DS
|
|
0
|
|
|
|
Reply
|
davids
|
7/14/2006 5:57:12 PM
|
|
Jack said the following, on 07/14/06 12:39:
> What is the difference betweeb the two functions? Thanks.
>
First, please put your question in the body of your message, not just in
the Subject: header; that ensures everyone can see it.
The two functions Jack refers to are open(2) and fopen(3).
The open(2) function is a Unix/Linux system call that opens (and
possibly creates) a specified file, and associates it with a file
descriptor (a small positive integer). It is standard in POSIX
environments.
fopen(3) is a function in the standard C library. It opens a specified
file and associates it with a C stream (referred to by a FILE * object).
There are, of course, similarities between the two functions. C, of
course, originated in the UNIX environment, and the C standard attempts
to preserve some features of that environment across platforms. There
is an interesting discussion of the issues involved in P.J. Plauger's
excellent book, _The Standard C Library_.
The man pages have all the details.
--
Rich Gibbs
richg74@gmail.com
"You can observe a lot by watching." -- Yogi Berra
|
|
0
|
|
|
|
Reply
|
Rich
|
7/14/2006 9:28:21 PM
|
|
Jack schrieb:
>open() and fopen()
> What is the difference betweeb the two functions? Thanks.
In short:
open is a system call under Unix/Linux for low level (unbuffered) I/O.
It should be used in conjuction with (the system calls) read, write,
close and uses a file descriptor to indicate an open file. The usage
pattern can be looked up by "man open". It often is
#include <fcntl.h>
int fd;
char * name;
...
fd=open(name, O_RDONLY);
...
fopen is part of the stdio library which implements buffered I/O over
the low level I/O supplied by the operating system. Use it with fread,
fwrite and fclose. fopen yields a file pointer, which should be treated
mostly as an opaque structure, although it (often) contains a file
descriptor which can be obtained in special cases. Use "man fopen"
#include <stdio.h>
FILE * fp;
char * name;
fp=fopen(name,"r");
...
Hubble.
|
|
0
|
|
|
|
Reply
|
Hubble
|
7/15/2006 2:04:04 PM
|
|
Hubble wrote:
> Jack schrieb:
>
> >open() and fopen()
> > What is the difference betweeb the two functions? Thanks.
>
> In short:
> open is a system call under Unix/Linux for low level (unbuffered) I/O.
> It should be used in conjuction with (the system calls) read, write,
> close and uses a file descriptor to indicate an open file. The usage
> pattern can be looked up by "man open". It often is
>
> #include <fcntl.h>
>
> int fd;
> char * name;
> ...
> fd=open(name, O_RDONLY);
> ...
>
>
> fopen is part of the stdio library which implements buffered I/O over
> the low level I/O supplied by the operating system. Use it with fread,
> fwrite and fclose. fopen yields a file pointer, which should be treated
> mostly as an opaque structure, although it (often) contains a file
> descriptor which can be obtained in special cases. Use "man fopen"
>
>
> #include <stdio.h>
> FILE * fp;
> char * name;
>
> fp=fopen(name,"r");
> ...
>
> Hubble.
Thanks a lot.
Jack
|
|
0
|
|
|
|
Reply
|
Jack
|
7/17/2006 12:51:05 AM
|
|
|
4 Replies
132 Views
(page loaded in 0.075 seconds)
|
|
|
|
|
|
|
|
|