For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > July 2006 > open() and fopen()









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author open() and fopen()
Jack

2006-07-14, 6:59 pm

What is the difference betweeb the two functions? Thanks.

davids@webmaster.com

2006-07-14, 6:59 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

Rich Gibbs

2006-07-14, 6:59 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


Hubble

2006-07-15, 6:59 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.

Jack

2006-07-16, 7:00 pm


Hubble wrote:
> Jack schrieb:
>
>
> 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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com