Date: Thu, 12 Mar 1998 10:28:25 +1030 From: Greg Lehey <grog@lemis.com> To: Doug White <dwhite@resnet.uoregon.edu>, Matthew Hagerty <wpub1@net-link.net> Cc: freebsd-questions@FreeBSD.ORG Subject: Re: open(), fopen(), and flock() Message-ID: <19980312102825.28496@freebie.lemis.com> In-Reply-To: <Pine.BSF.3.96.980311104029.15787P-100000@gdi.uoregon.edu>; from Doug White on Wed, Mar 11, 1998 at 10:46:13AM -0800 References: <3.0.3.32.19980310163441.031536ec@smtp.net-link.net> <Pine.BSF.3.96.980311104029.15787P-100000@gdi.uoregon.edu>
next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, 11 March 1998 at 10:46:13 -0800, Doug White wrote: > 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. :) In fact, there's very little difference in usage between open/read/write/close and fopen/fread/fwrite/fclose. The big difference is in the fact that the f* functions are buffered library functions, whereas the others are system calls. This improves performance if you're accessing a lot of unaligned data (the normal case). It also makes locking impossible, since you're keeping private copies of the data. Greg 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?19980312102825.28496>