fread fwrite之间是否一定要fseek,为什么?高手进

coolboycool 2011-03-11 09:49:54
通常,在操作文件的时候,会同时使用到fread 和 fwrite ,但记得在 <<C陷阱和缺陷>>中有提到一句,fread和fwirte混用时,一定要使用fseek().

当然,通常我们使用fseek是因为要偏移到我们想要操作的地方,如果当前fread完的位置正好是我们要fwrite的位置时,是否也必需加上fseek()?
为什么?
如下例:
1:while( NL_read( fd, &key, sizeof( ST_UNRD_key )) == sizeof( ST_UNRD_key )){
2: NL_seek( fd, -sizeof( ST_UNRD_key ), FILE_SEEK_CUR);
3: NL_write( fd, &key, sizeof( ST_UNRD_key ) );
4: NL_seek( fd, 0, FILE_SEEK_CUR );
5:}


关于第4行:
1:必要 (原因?)
2:建议需要 (原因?)
3:根本不需要 (原因?)
4:打酱油路过 (原因2...)

感谢回答
...全文
603 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
LinuxBirdMan 2011-03-11
  • 打赏
  • 举报
回复
打酱油的原因。。蹭分。。。呵呵。。上面的都说的很详细了。。
赵4老师 2011-03-11
  • 打赏
  • 举报
回复
以下内容摘自MSDN98:
fopen, _wfopen
Open a file.

FILE *fopen( const char *filename, const char *mode );

FILE *_wfopen( const wchar_t *filename, const wchar_t *mode );

Function Required Header Compatibility
fopen <stdio.h> ANSI, Win 95, Win NT
_wfopen <stdio.h> or <wchar.h> 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


The c, n, and t mode options are Microsoft extensions for fopen and _fdopen and should not be used where ANSI portability is desired.

Return Value

Each of these functions returns a pointer to the open file. A null pointer value indicates an error.

Parameters

filename

Filename

mode

Type of access permitted

Remarks

The fopen function opens the file specified by filename. _wfopen is a wide-character version of fopen; the arguments to _wfopen are wide-character strings. _wfopen and fopen behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tfopen fopen fopen _wfopen


The character string mode specifies the type of access requested for the file, as follows:

"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 given 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 given file exists, its contents are destroyed.

"a+"

Opens for reading and appending; the appending operation includes the removal of the EOF marker before new 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 or rewind, but is always moved back to the end of the file before any 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 has occurred, 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 does remove 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, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.
justkk 2011-03-11
  • 打赏
  • 举报
回复
这个应该与标准IO库的实现机制有关,具体原因不详
justkk 2011-03-11
  • 打赏
  • 举报
回复
必要

摘自C语言缺陷与陷阱C Traps and Pitfalls_cat paw_百度空间

书上只是说:第二个fseek函数虽然看上去什么也没做,但它改变了文件的状态,使得文件现在可以正常地进行读取了
quwei197874 2011-03-11
  • 打赏
  • 举报
回复
必须的,微软作的封装.
bdmh 2011-03-11
  • 打赏
  • 举报
回复
因为fread和fwrite都会移动文件内部指针,所以如果使用了这两个方法,你就要考虑fseek回原来的位置,当然用不用,你自己决定
yyg990441 2011-03-11
  • 打赏
  • 举报
回复
另外,如果文件不是很大,一般还是把全部内容读到数组里,在数组里修改完再写回文件。
lx3275852 2011-03-11
  • 打赏
  • 举报
回复
回答:1.必要的,原因:

因为这个实在while循环里
如果不用seek的话,下一次执行while里边的NL_read( fd, &key, sizeof( ST_UNRD_key )) == sizeof( ST_UNRD_key ),就会出错!~

你可以这么理解:
seek是改变文件读取位置
读文件,要改变文件读取位置,写文件依然要改变文件读取位置。。
所以用一个open来实现读写混用的时候,必须seek

当然如果你不愿意用seek的话,可以用两个open来实现,因为两个open就有两个指针,两个指针互不影响于是乎,没有任何压力~~
yyg990441 2011-03-11
  • 打赏
  • 举报
回复
必要

对一个文件以读写方式打开,如果你先执行读操作,然后又想执行写操作,在读写操作之间必须要fseek()一下以使文件流改变状态(从读方式装换到写方式)
solohac 2011-03-11
  • 打赏
  • 举报
回复
你的做法:
read
seek
write
seek

我的做法
seek
read
seek
write
先seek后操作
coolboycool 2011-03-11
  • 打赏
  • 举报
回复
分数已按回答内容的权重散了,感谢各位帮忙,很清晰了...
另特给10楼打酱油的1分,以资鼓励 哈哈

69,433

社区成员

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

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