关于文件的读写操作,该如何解决

关于文件的读写操作
大家好,本人的问题是:
  
     由于本人需要写多个.cpp文件,其中每个.cpp文件中都需要向同一个文件写数据,所以我采用fopen函数
打开文件,fwrite函数写数据。问题是,比如1.cpp中写了数据到文件file.txt,然后fclose关闭。当2.cpp文件
向file.txt文件写数据时,只能是按照fopen函数的"ab"或"wb"的方式,要么是在最后位置写,要么是将原来的数据清零后重新写,而不能用fseek函数定位到指定位置写入数据。 本人猜想是由于fclose释放了所有文件资源的缘故,但是如果我要实现文件定位怎么办,难道是打开文件后就一直不关闭? 求赐教,谢谢~~
------解决方案--------------------
二进制方式读写文件操作而已!
每次打开文件关闭文件后,再打开的话,文件读指针就在文件开头了!所以覆盖是正常的!
所以第二次打开文件的时候可以用fseek来偏移指针的!或者可以打开写操作后,不立即关闭文件,继续写操作最好,最后写完再关闭,节约系统调用的消耗
------解决方案--------------------
判断下你打开的返回值吧!估计你都没有成功打开文件呢!

注意windows下"\"是个逃逸字符,

路径用"\\";
------解决方案--------------------
_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,