user-0 提问: fseek函数将文件指针设置到文件尾之后会产生什么行为？

回复:
user: user-1
content: fseek定位到文件尾部，如果是文件读取，则会返回EOF；如果是文件写入，则会在文件尾部追加数据。一般将文件定位到文件尾部有这么几个应用场景： 1. 获取文件大小。即首先用fseek到文件末尾，然后用ftell就可以知道文件占用了多少字节的空间。 2. 读取相对于文件末尾的某个重要字段，类似Magic Number，Magic Number一般是在文件头部，但是如果有某种文件规定文件末尾的几个字节有特殊含义，这是要读取这些字节就会定位到文件尾部。

user: user-2
content: 仔细读：http://en.cppreference.com/w/c/io/fseek

user: user-0
content: 引用 1 楼 zjq9931的回复:跳到文件尾，就是跳到文件尾了啊。 读是读不到了，只能写。 文件大小只有3K，继续写的时候却想从9K的地方写。 那么3K-9K之间你打算填充空白吗？你需要定义什么是空白，什么是空白你就往那一段里面写入什么，就好了。 谢谢，明白你的意思了，在文件尾追加空白数据，直到9KB处再写入新数据。

user: user-3
content: 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

user: user-4
content: 跳到文件尾，就是跳到文件尾了啊。 读是读不到了，只能写。 文件大小只有3K，继续写的时候却想从9K的地方写。 那么3K-9K之间你打算填充空白吗？你需要定义什么是空白，什么是空白你就往那一段里面写入什么，就好了。