回复: 【提问】QT如何锁一个文件,不让其它程序读或者写??
#6 [XChinux 10-31 14:47]
lock()函数,是C库函数中的东西.
物在BCB6的Help中查到.
Win32也能用.
Header File
io.h
Category
Input/output Routines
Prototype
int lock(int handle, long offset, long length);
Description
Sets file-sharing locks.
lock provides an interface to the operating system file-sharing mechanism.
A lock can be placed on arbitrary, nonoverlapping regions of any file. A program attempting to read or write into a locked region will retry the operation three times. If all three retries fail, the call fails with an error.
Return Value
lock returns 0 on success. On error, lock returns -1 and sets the global variable errno to
EACCES Locking violation
#7 [XChinux 10-31 14:48]
下面是BCB6 Help中的lock()函数的Example
#include
#include
#include
#include
#include
#include
int main(void)
{
int handle, status;
long length;
handle = _sopen("c:\\autoexec.bat",
O_RDONLY,SH_DENYNO,S_IREAD);
if (handle < 0)
{
printf("_sopen failed\n");
exit(1);
}
length = filelength(handle);
status = lock(handle,0L,length/2);
if (status == 0)
printf("lock succeeded\n");
else
printf("lock failed\n");
status = unlock(handle,0L,length/2);
if (status == 0)
printf("unlock succeeded\n");
else
printf("unlock failed\n");
close(handle);
return 0;
}
#8 [hetal 10-31 15:32]
我的意思是想问QT下如何锁一个文件,其它的方法我也有了
QtLockedFile确认是可以的,不过下载时要账号和密码,我有Trolltech Qt Solutions v3.3.2 Final 正式版,但是里面没有包含QtLockedFile类
#9 [XChinux 10-31 15:34]
Qt开源版没有提供那就用不上了.
用C/C++标准库里的东西会比Qt的东西更好.可移植性更强.
#10 [hetal 11-01 10:30]
看起来我只有去找一个商业版本了