Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 Mar 1998 10:46:13 -0800 (PST)
From:      Doug White <dwhite@gdi.uoregon.edu>
To:        Matthew Hagerty <wpub1@net-link.net>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: open(), fopen(), and flock()
Message-ID:  <Pine.BSF.3.96.980311104029.15787P-100000@gdi.uoregon.edu>
In-Reply-To: <3.0.3.32.19980310163441.031536ec@smtp.net-link.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, 10 Mar 1998, Matthew Hagerty wrote:

>    Could someone tell me if you can use flock() to lock a file opened with
> fopen()?  

No; flock() wants a file descriptor; see the man page. Use open(), flock() 
it, then fdopen() the descriptor if you want to use the f* functions.  I
don't think fdopen() eats the file descriptor though, so you should be
able to flock() the descriptor all you want even after you fdopen() it. 

>    Is there some reason to use fopen() over open() or vice/versa?  Is there
> even any locking mechanisms in ANSI-C?

fopen() lets you use the f* functions that take FILE* as arguments
(fprintf(), fscanf(), etc.) and lets you do file repositioning easily
(rewind() & friends). Otherwise you have to use read()/write() directly,
and they are kinda messy for complex file operations.  They're kinda messy
in general. :)

I don't know about any standardized file locking operations; that sort of
thing would fall under POSIX.

Doug White                              | University of Oregon  
Internet:  dwhite@resnet.uoregon.edu    | Residence Networking Assistant
http://gladstone.uoregon.edu/~dwhite    | Computer Science Major



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.3.96.980311104029.15787P-100000>