feek()函数

Rookiekk 2017-03-14 11:03:05
函数调用
feek(fp,10L,2);这个2代表代表seek_end?
另外如果是代表文件结尾,再向前移动10个字节会到哪里啊。
seek_set是0,seek_cur是1,seek_end是2,是这个顺序吧? 求大神指点
...全文
408 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rookiekk 2017-03-15
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
fseek Moves the file pointer to a specified location. int fseek( FILE *stream, long offset, int origin ); Function Required Header Compatibility fseek <stdio.h> ANSI, 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 If successful, fseek returns 0. Otherwise, it returns a nonzero value. On devices incapable of seeking, the return value is undefined. Parameters stream Pointer to FILE structure offset Number of bytes from origin origin Initial position Remarks The fseek function moves the file pointer (if any) associated with stream to a new location that is offset bytes from origin. The next operation on the stream takes place at the new location. On a stream open for update, the next operation can be either a read or a write. The argument origin must be one of the following constants, defined in STDIO.H: SEEK_CUR Current position of file pointer SEEK_END End of file SEEK_SET Beginning of file You can use fseek to reposition the pointer anywhere in a file. The pointer can also be positioned beyond the end of the file. fseek clears the end-of-file indicator and negates the effect of any prior ungetc calls against stream. When a file is opened for appending data, the current file position is determined by the last I/O operation, not by where the next write would occur. If no I/O operation has yet occurred on a file opened for appending, the file position is the start of the file. For streams opened in text mode, fseek has limited use, because carriage return–linefeed translations can cause fseek to produce unexpected results. The only fseek operations guaranteed to work on streams opened in text mode are: Seeking with an offset of 0 relative to any of the origin values. Seeking from the beginning of the file with an offset value returned from a call to ftell. Also in text mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing, fopen and all related routines check for a CTRL+Z at the end of the file and remove it if possible. This is done because using fseek and ftell to move within a file that ends with a CTRL+Z may cause fseek to behave improperly near the end of the file. Example /* FSEEK.C: This program opens the file FSEEK.OUT and * moves the pointer to the file's beginning. */ #include <stdio.h> void main( void ) { FILE *stream; char line[81]; int result; stream = fopen( "fseek.out", "w+" ); if( stream == NULL ) printf( "The file fseek.out was not opened\n" ); else { fprintf( stream, "The fseek begins here: " "This is the file 'fseek.out'.\n" ); result = fseek( stream, 23L, SEEK_SET); if( result ) perror( "Fseek failed" ); else { printf( "File pointer is set to middle of first line.\n" ); fgets( line, 80, stream ); printf( "%s", line ); } fclose( stream ); } } Output File pointer is set to middle of first line. This is the file 'fseek.out'. Stream I/O Routines See Also ftell, _lseek, rewind
谢谢老鼠
赵4老师 2017-03-15
  • 打赏
  • 举报
回复
fseek Moves the file pointer to a specified location. int fseek( FILE *stream, long offset, int origin ); Function Required Header Compatibility fseek <stdio.h> ANSI, 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 If successful, fseek returns 0. Otherwise, it returns a nonzero value. On devices incapable of seeking, the return value is undefined. Parameters stream Pointer to FILE structure offset Number of bytes from origin origin Initial position Remarks The fseek function moves the file pointer (if any) associated with stream to a new location that is offset bytes from origin. The next operation on the stream takes place at the new location. On a stream open for update, the next operation can be either a read or a write. The argument origin must be one of the following constants, defined in STDIO.H: SEEK_CUR Current position of file pointer SEEK_END End of file SEEK_SET Beginning of file You can use fseek to reposition the pointer anywhere in a file. The pointer can also be positioned beyond the end of the file. fseek clears the end-of-file indicator and negates the effect of any prior ungetc calls against stream. When a file is opened for appending data, the current file position is determined by the last I/O operation, not by where the next write would occur. If no I/O operation has yet occurred on a file opened for appending, the file position is the start of the file. For streams opened in text mode, fseek has limited use, because carriage return–linefeed translations can cause fseek to produce unexpected results. The only fseek operations guaranteed to work on streams opened in text mode are: Seeking with an offset of 0 relative to any of the origin values. Seeking from the beginning of the file with an offset value returned from a call to ftell. Also in text mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing, fopen and all related routines check for a CTRL+Z at the end of the file and remove it if possible. This is done because using fseek and ftell to move within a file that ends with a CTRL+Z may cause fseek to behave improperly near the end of the file. Example /* FSEEK.C: This program opens the file FSEEK.OUT and * moves the pointer to the file's beginning. */ #include <stdio.h> void main( void ) { FILE *stream; char line[81]; int result; stream = fopen( "fseek.out", "w+" ); if( stream == NULL ) printf( "The file fseek.out was not opened\n" ); else { fprintf( stream, "The fseek begins here: " "This is the file 'fseek.out'.\n" ); result = fseek( stream, 23L, SEEK_SET); if( result ) perror( "Fseek failed" ); else { printf( "File pointer is set to middle of first line.\n" ); fgets( line, 80, stream ); printf( "%s", line ); } fclose( stream ); } } Output File pointer is set to middle of first line. This is the file 'fseek.out'. Stream I/O Routines See Also ftell, _lseek, rewind
赵4老师 2017-03-15
  • 打赏
  • 举报
回复
到了结尾下边10个字节的地方
www_adintr_com 2017-03-14
  • 打赏
  • 举报
回复
如果你是写文件, 文件的长度会自动扩展, 中间的部分内容当做 0 在 UNIX 下, 利用这种方式可以制作文件空洞, 就是文件实际占用的磁盘空间比文件的大小更小.
自信男孩 2017-03-14
  • 打赏
  • 举报
回复
int fseek(FILE * stream, long offset, int whence);
1. SEEK_SET 从距文件开头offset 位移量为新的读写位置. 2. SEEK_CUR 以目前的读写位置往后增加offset 个位移量. 3. SEEK_END 将读写位置指向文件尾后再增加offset 个位移量. 当whence 值为SEEK_CUR 或 4. SEEK_END 时, 参数offset 允许负值的出现 0, 1, 2分别是SEEK_SET, SEEK_CUR, SEEK_END
Rookiekk 2017-03-14
  • 打赏
  • 举报
回复
引用 2 楼 adlay 的回复:
如果你是写文件, 文件的长度会自动扩展, 中间的部分内容当做 0 在 UNIX 下, 利用这种方式可以制作文件空洞, 就是文件实际占用的磁盘空间比文件的大小更小.
老师您好,我已经了解了那几个词的定义。但是在文件结尾再向前移动10个字节,那个位置到哪里了呢?是到了结尾下边10个字节的地方,还是会自动回到开头在移动10个字节呢?
Rookiekk 2017-03-14
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
VS IDE中,在不明白的符号上点鼠标右键,选转到定义。
老师您好,我已经了解了那几个词的定义。但是在文件结尾再向前移动10个字节,那个位置到哪里了呢?是到了结尾下边10个字节的地方,还是会自动回到开头在移动10个字节呢?
赵4老师 2017-03-14
  • 打赏
  • 举报
回复
VS IDE中,在不明白的符号上点鼠标右键,选转到定义。

69,369

社区成员

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

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