This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
File locking with gcc 3
- From: Chris Blake <Chris dot Blake at anu dot edu dot au>
- To: gcc-help at gcc dot gnu dot org
- Date: Tue, 15 Jan 2002 08:20:40 +1100 (EST)
- Subject: File locking with gcc 3
How does one do Posix file locking with gcc3 / libstdc++-v3?
The program is written in C++, and I'm trying to compile it
under Solaris 8. It is a simple booking system where the program
locks a booking data file, reads the last request number from it,
then writes a new booking with an incremented request number before
releasing the lock.
The following fragment used to compile under g++ 2.95...
-----------------------
#define GCC3 1
#if GCC3 // mods for gcc 3
#include <iostream>
#include <fstream>
#include <fcntl.h>
#include <cstdio>
#include <cctype>
using namespace std;
#else
#include <fstream.h>
#include <fcntl.h> // for file locking
#include <stdio.h>
#include <ctype.h>
#endif
int JobRequest::getlock()
{
struct flock lock;
fd = jobfile.rdbuf()->fd();
lock.l_type = F_WRLCK;
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
return( fcntl(fd, F_SETLK, &lock) );
}
-----------------------
....but with g++ 3.0.2 I get
request.cpp: In member function `int JobRequest::getlock()':
request.cpp:70: no matching function for call to `std::basic_filebuf<char,
std::char_traits<char> >::fd()'
I've also tried replacing the body of the function with the line
return( fcntl(fd, F_SETLKW, file_lock(F_WRLCK, SEEK_SET)));
but g++ then gives the error...
request.cpp:79: `file_lock' undeclared (first use this function)
...and file_lock() does not appear to be defined in any of the system
include files.
Perhaps there is another method of locking files?
thanks,
Chris Blake