如何锁定文件

pp1982 2011-01-27 01:31:00
我的程序在修改一个文件的内容,是分段修改的,有时候会出现修改到某段的时候下面的修改就失效。
比如文件大小 1024 K,我每次修改 128 K,可能会出现 修改了 128 * 7 后,最后的 128 K 没能被修改。
所以我的想法,有没有办法做类似于锁定,或者事务这样的操作,谢谢
...全文
318 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
pp1982 2011-02-06
  • 打赏
  • 举报
回复
是这样的,文件是 “读取一部分修改一部分”,这个是由于特殊需求这么做的,而且这个文件是在被别的程序修改过程中,我对他进行的操作。 大部分时候是没问题的,但是偶尔会出现我问题中问到的问题。
weerweer 2011-01-27
  • 打赏
  • 举报
回复
文件锁? 感觉lz的问题有些不清楚,不是多进程同时读文件造成的。

还是lz的文件操作方法有问题?
a707000646 2011-01-27
  • 打赏
  • 举报
回复
犀利楼上牛人
赵4老师 2011-01-27
  • 打赏
  • 举报
回复
_locking
Locks or unlocks bytes of a file.

int _locking( int handle, int mode, long nbytes );

Routine Required Header Optional Headers Compatibility
_locking <io.h> and <sys/locking.h> <errno.h> Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

_locking returns 0 if successful. A return value of –1 indicates failure, in which case errno is set to one of the following values:

EACCES

Locking violation (file already locked or unlocked).

EBADF

Invalid file handle.

EDEADLOCK

Locking violation. Returned when the _LK_LOCK or _LK_RLCK flag is specified and the file cannot be locked after 10 attempts.

EINVAL

An invalid argument was given to _locking.

Parameters

handle

File handle

mode

Locking action to perform

nbytes

Number of bytes to lock

Remarks

The _locking function locks or unlocks nbytes bytes of the file specified by handle. Locking bytes in a file prevents access to those bytes by other processes. All locking or unlocking begins at the current position of the file pointer and proceeds for the next nbytes bytes. It is possible to lock bytes past end of file.

mode must be one of the following manifest constants, which are defined in LOCKING.H:

_LK_LOCK

Locks the specified bytes. If the bytes cannot be locked, the program immediately tries again after 1 second. If, after 10 attempts, the bytes cannot be locked, the constant returns an error.

_LK_NBLCK

Locks the specified bytes. If the bytes cannot be locked, the constant returns an error.

_LK_NBRLCK

Same as _LK_NBLCK.

_LK_RLCK

Same as _LK_LOCK.

_LK_UNLCK

Unlocks the specified bytes, which must have been previously locked.

Multiple regions of a file that do not overlap can be locked. A region being unlocked must have been previously locked. _locking does not merge adjacent regions; if two locked regions are adjacent, each region must be unlocked separately. Regions should be locked only briefly and should be unlocked before closing a file or exiting the program.

Example

/* LOCKING.C: This program opens a file with sharing. It locks
* some bytes before reading them, then unlocks them. Note that the
* program works correctly only if the file exists.
*/

#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/locking.h>
#include <share.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

void main( void )
{
int fh, numread;
char buffer[40];

/* Quit if can't open file or system doesn't
* support sharing.
*/
fh = _sopen( "locking.c", _O_RDWR, _SH_DENYNO,
_S_IREAD | _S_IWRITE );
if( fh == -1 )
exit( 1 );

/* Lock some bytes and read them. Then unlock. */
if( _locking( fh, LK_NBLCK, 30L ) != -1 )
{
printf( "No one can change these bytes while I'm reading them\n" );
numread = _read( fh, buffer, 30 );
printf( "%d bytes read: %.30s\n", numread, buffer );
lseek( fh, 0L, SEEK_SET );
_locking( fh, LK_UNLCK, 30L );
printf( "Now I'm done. Do what you will with them\n" );
}
else
perror( "Locking failed\n" );

_close( fh );
}


Output

No one can change these bytes while I'm reading them
30 bytes read: /* LOCKING.C: This program ope
Now I'm done. Do what you will with them


File Handling Routines

See Also _creat, _open
jaylong35 2011-01-27
  • 打赏
  • 举报
回复
首先要看你是如何处理这个修改的,是把全部数据都读取到内存中修改好了,再写入,还是读取一部分何必一部分。
你可以在修改前做一个备份,如果修改成功就返回,要不还原。
还有一种方法,就是记录下你的修改操作,以及修改过的数据,做一个日志
如果修改失败,就逆向执行,恢复。
如果是要完全恢复,还是备份效率高。

锁文件可以有好多方法,比如,以独占的方式打开,另外,还可以在文件目录下面新建一个,锁文件,在打开文件前判断这个文件是否被占用。如果是就锁了,如果没有,那就没锁。
bdmh 2011-01-27
  • 打赏
  • 举报
回复
CreateFile时,选择权限,不允许其他程序访问,不过感觉还是你的代码有问题
cwbcwb505 2011-01-27
  • 打赏
  • 举报
回复
CreateFile
bdmh 2011-01-27
  • 打赏
  • 举报
回复
你怎么修改的,难道还有多个程序对其操作吗

69,374

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧