user-0 提问: 关于fopen过程中，打开的源文件被修改或者删除，会有什么后果

回复:
user: user-1
content: 不行的，只要打开了在读的时候，其他人最多也只能打开读，是不能写和删除的。 平时用的软件打开了一个文件后其他人还可以写和删除那是因为打开文件的软件是打开后就把文件对到内存然后就关闭了文件了。

user: user-2
content: 可以捕捉这个错误，如果出现错误，提示XXX  楼主不用担心误读了硬盘其他地方，如果删了，他再读肯定会出错

user: user-2
content: 如果这样，会出现错误，可以用捕捉到，文件读取写入的错误类型有一大堆，楼主可以自己试下就知了，也可以参看MSDN

user: user-0
content: 引用 2 楼 ForestDB 的回复:应该删除不了才对。  可以用独占方式打开看看。 假设我们不是独占打开，可以修改和删除，会有什么后果

user: user-3
content: 应该删除不了才对。  可以用独占方式打开看看。

user: user-0
content: 在线等！！！！

user: user-4
content: _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

user: user-0
content: 引用 7 楼 henuyx 的回复:如果是我设计fopen系列的函数：   fopen之后，我会把这个文件放在一个保护区   即使你删了，也只是ls看不到了，但是实际上这个文件还在，但不会增加新的链接数   直到打开这个文件的链接数为0之后，再将这个文件真正的删除了！   这样就不会出现你说的那种疑问了。   可以测试一下。   ------------------------------------------------------------------------   个人理解  你的这个推测离真相很近，测试结果也是这样，即使删除了，还是会继续处理，删除我不是很担心，我担心的是如果被覆盖或修改会不会有误读的风险？

user: user-5
content: 如果是我设计fopen系列的函数：   fopen之后，我会把这个文件放在一个保护区   即使你删了，也只是ls看不到了，但是实际上这个文件还在，但不会增加新的链接数   直到打开这个文件的链接数为0之后，再将这个文件真正的删除了！   这样就不会出现你说的那种疑问了。   可以测试一下。      ------------------------------------------------------------------------   个人理解