fclose关闭文件后如何再打开同一个文件到某个偏移位置再写

fclose关闭文件后怎么再打开同一个文件到某个偏移位置再写
大家好,本人的问题是:
  
     由于本人需要写多个.cpp文件,其中每个.cpp文件中都需要向同一个文件写数据,所以我采用fopen函数
打开文件,fwrite函数写数据。问题是,比如1.cpp中写了数据到文件file.txt,然后fclose关闭。当2.cpp文件
向file.txt文件写数据时,只能是按照fopen函数的"ab"或"wb"的方式,要么是在最后位置写,要么是将原来的数据清零后重新写,而不能用fseek函数定位到指定位置写入数据。 本人猜想是由于fclose释放了所有文件资源的缘故,但是如果我要实现文件定位怎么办,难道是打开文件后就一直不关闭? 求赐教,谢谢~~
c/c++  数据 文件操作

------解决方案--------------------

二进制方式读写文件操作而已!
每次打开文件关闭文件后,再打开的话,文件读指针就在文件开头了!所以覆盖是正常的!
所以第二次打开文件的时候可以用fseek来偏移指针的!或者可以打开写操作后,不立即关闭文件,继续写操作最好,最后写完再关闭,节约系统调用的消耗 

------解决方案--------------------
关闭之前把偏移记下来,打开后再恢复
------解决方案--------------------
楼主需要仔细的看MSDN呀, 上面都有说的. 红色部分就是你想要的.

"r" 
Opens for reading. If the file does not exist or cannot be found, the fopen call fails. 
"w" 
Opens an empty file for writing. If the file exists, its contents are destroyed. 
"a" 
Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn't exist. 
"r+" 
Opens for both reading and writing. (The file must exist.)
 
"w+" 
Opens an empty file for both reading and writing. If the file exists, its contents are destroyed. 
"a+" 
Opens for reading and appending; the appending operation includes the removal of the EOF marker before data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn't exist. 
When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. 

The file pointer can be repositioned using fseek, but is always moved back to the end of the file before a write operation is carried out. Thus, existing data cannot be overwritten. 

The "a" mode does not remove the EOF marker before appending to the file. After appending occurs, the MS-DOS TYPE command only shows data up to the original EOF marker and not any data appended to the file. 

The "a+" mode removes the EOF marker before appending to the file. After appending, the MS-DOS TYPE command shows all data in the file. 

The "a+" mode is required for appending to a stream file that is terminated with the CTRL+Z EOF marker. 

When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, or fseek operation. 

The current position can be specified for the fsetpos or fseek operation. 

In addition to the above values, the following characters can be included in mode to specify the translation mode for newline characters: 


Open in text (translated) mode. 
In this mode, CTRL+Z is interpreted as an end-of-file character on input. 

In files opened for reading/writing with "a+", fopen checks for a CTRL+Z at the end of the file and removes it, if possible. 

This is done because using fseek and ftell to move within a file that ends with a CTRL+Z can cause fseek to behave improperly near the end of the file.