Juzzam
asked on
Lock a file in Borland C++ (Command Line Version)
I'm using Borland C++ 5.5.1 (Command Line Version) under Windows 98
I need to lock a file (make it Read-Only) and then unlock it after some modifications are made to it in my program.
Thanks in advance,
Robby
I need to lock a file (make it Read-Only) and then unlock it after some modifications are made to it in my program.
Thanks in advance,
Robby
ASKER
Looks like unix only...
this is what I tried:
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <cstring.h>
#include <function.h>
#include <string.h>
#include <iostream.h>
#include <fcntl.h>
int main()
{
fd = open("myfile.txt", O_RDWR | O_CREAT);
if(fd == -1)
{
switch(errno)
{
case EACCES:
throw std::runtime_error("You do not have access to this file.");
case EISDIR:
throw std::runtime_error("Cannot use a directory");
default:
throw std::runtime_error("Unknow n error occurred.");
}
}
// Set a read/write lock on the file
struct flock fl;
fl.l_type = F_WRLCK | F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = getpid();
fcntl(fd, F_SETLKW, fl); // Wait for a Lock
}
-------------------------- ---------- ---------- ---------- -----
ERRORS:
-------------------------- ---------- ---------- ---------- -----
C:\Borland\BCC55\Bin>bcc32 read_only
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
read_only.cpp:
Error E2451 read_only.cpp 13: Undefined symbol 'fd' in function main()
Error E2268 read_only.cpp 13: Call to undefined function 'open' in function main
()
Error E2451 read_only.cpp 19: Undefined symbol 'EACCES' in function main()
Error E2451 read_only.cpp 22: Undefined symbol 'EISDIR' in function main()
Error E2172 read_only.cpp 22: Duplicate case in function main()
Error E2450 read_only.cpp 31: Undefined structure 'flock' in function main()
Error E2449 read_only.cpp 31: Size of 'fl' is unknown or zero in function main()
Error E2450 read_only.cpp 31: Undefined structure 'flock' in function main()
Error E2315 read_only.cpp 33: 'l_type' is not a member of 'flock', because the t
ype is not yet defined in function main()
Error E2451 read_only.cpp 33: Undefined symbol 'F_WRLCK' in function main()
Error E2451 read_only.cpp 33: Undefined symbol 'F_RDLCK' in function main()
Error E2315 read_only.cpp 34: 'l_whence' is not a member of 'flock', because the
type is not yet defined in function main()
Error E2315 read_only.cpp 35: 'l_start' is not a member of 'flock', because the
type is not yet defined in function main()
Error E2315 read_only.cpp 36: 'l_len' is not a member of 'flock', because the ty
pe is not yet defined in function main()
Error E2315 read_only.cpp 37: 'l_pid' is not a member of 'flock', because the ty
pe is not yet defined in function main()
Error E2268 read_only.cpp 37: Call to undefined function 'getpid' in function ma
in()
Error E2268 read_only.cpp 39: Call to undefined function 'fcntl' in function mai
n()
Error E2451 read_only.cpp 39: Undefined symbol 'F_SETLKW' in function main()
*** 18 errors in Compile ***
this is what I tried:
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <cstring.h>
#include <function.h>
#include <string.h>
#include <iostream.h>
#include <fcntl.h>
int main()
{
fd = open("myfile.txt", O_RDWR | O_CREAT);
if(fd == -1)
{
switch(errno)
{
case EACCES:
throw std::runtime_error("You do not have access to this file.");
case EISDIR:
throw std::runtime_error("Cannot
default:
throw std::runtime_error("Unknow
}
}
// Set a read/write lock on the file
struct flock fl;
fl.l_type = F_WRLCK | F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = getpid();
fcntl(fd, F_SETLKW, fl); // Wait for a Lock
}
--------------------------
ERRORS:
--------------------------
C:\Borland\BCC55\Bin>bcc32
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
read_only.cpp:
Error E2451 read_only.cpp 13: Undefined symbol 'fd' in function main()
Error E2268 read_only.cpp 13: Call to undefined function 'open' in function main
()
Error E2451 read_only.cpp 19: Undefined symbol 'EACCES' in function main()
Error E2451 read_only.cpp 22: Undefined symbol 'EISDIR' in function main()
Error E2172 read_only.cpp 22: Duplicate case in function main()
Error E2450 read_only.cpp 31: Undefined structure 'flock' in function main()
Error E2449 read_only.cpp 31: Size of 'fl' is unknown or zero in function main()
Error E2450 read_only.cpp 31: Undefined structure 'flock' in function main()
Error E2315 read_only.cpp 33: 'l_type' is not a member of 'flock', because the t
ype is not yet defined in function main()
Error E2451 read_only.cpp 33: Undefined symbol 'F_WRLCK' in function main()
Error E2451 read_only.cpp 33: Undefined symbol 'F_RDLCK' in function main()
Error E2315 read_only.cpp 34: 'l_whence' is not a member of 'flock', because the
type is not yet defined in function main()
Error E2315 read_only.cpp 35: 'l_start' is not a member of 'flock', because the
type is not yet defined in function main()
Error E2315 read_only.cpp 36: 'l_len' is not a member of 'flock', because the ty
pe is not yet defined in function main()
Error E2315 read_only.cpp 37: 'l_pid' is not a member of 'flock', because the ty
pe is not yet defined in function main()
Error E2268 read_only.cpp 37: Call to undefined function 'getpid' in function ma
in()
Error E2268 read_only.cpp 39: Call to undefined function 'fcntl' in function mai
n()
Error E2451 read_only.cpp 39: Undefined symbol 'F_SETLKW' in function main()
*** 18 errors in Compile ***
sorry, you will need to define
FILE* fd;
at the beginning.
Also, EACCES/EISDIR is in <io.h> in borland C++.
and you want to use
_rtl_open(filename, O_RDRW | SH_DENYRW)
instead of open
FILE* fd;
at the beginning.
Also, EACCES/EISDIR is in <io.h> in borland C++.
and you want to use
_rtl_open(filename, O_RDRW | SH_DENYRW)
instead of open
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
fd = open(filename.c_str(), O_RDWR | O_CREAT);
if(fd == -1)
{
switch(errno)
{
case EACCES:
throw std::runtime_error("You do not have access to this file.");
case EISDIR:
throw std::runtime_error("Cannot
default:
throw std::runtime_error("Unknow
}
}
// Set a read/write lock on the file
struct flock fl;
fl.l_type = F_WRLCK | F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = getpid();
fcntl(fd, F_SETLKW, fl); // Wait for a Lock