fseek 是做什么的?
What does fseek do?
我是 C 的新手,正在使用 fseek。我的问题是,它到底有什么作用?
例如:fseek(FILE *stream, -1, SEEK_CUR)
将向后读取 1 个字符,那么它是否已经在文件末尾?
这是否意味着 fseek(FILE *stream, 0, SEEK_CUR)
什么都不做?
我们如何将 fseek
之后的函数应用于数据结构?
I'm new to C and am using fseek
. My question is, what exactly does it do?
fseek
为 stream
指向的流设置文件位置指示符。新位置将由偏移量结合标志给出:
对于SEEK_SET
,偏移量是相对于文件的开头。
对于SEEK_CUR
,偏移量是相对于当前指标位置的。
对于SEEK_END
它是相对于文件结尾的。
eg: fseek(FILE *stream, -1, SEEK_CUR)
will read 1 character backwards, so is it already at the end of the file?
可能在开头,可能在中间,也可能在结尾,当前位置就是现在所在的位置。如果指标位于文件末尾,它将向后移动一个字节。
它不读取字符,如前所述,它将指示器从当前位置偏移到所需位置,如上所述。
Does that mean fseek(FILE *stream, 0, SEEK_CUR)
does nothing?
实际上它没有做任何可见的事情,因为偏移量为 0 它不移动指标,它只是将它放在相同的位置。它确实清除文件结束状态(重置 EOF
标志 )。
来自 C11 N1570 §7.21.9.2
#include <stdio.h>
int fseek(FILE *stream, long int offset, int whence);
Description:
2 The fseek
function sets the file position indicator for the stream pointed to by stream
. If a read or write error occurs, the error indicator for the stream is set and fseek
fails.
3 For a binary stream, the new position, measured in characters from the beginning of the file, is obtained by adding offset to the position specified by whence
. The specified position is the beginning of the file if whence
is SEEK_SET
, the current value of the file position indicator if SEEK_CUR
, or end-of-file if SEEK_END
. A binary stream need not meaningfully support fseek calls with a whence
value of SEEK_END
.
4 For a text stream, either offset
shall be zero, or offset
shall be a value returned by an earlier successful call to the ftell function on a stream associated with the same file and whence
shall be SEEK_SET
.
5 After determining the new position, a successful call to the fseek
function undoes any effects of the ungetc function on the stream, clears the end-of-file indicator for the stream, and then establishes the new position. After a successful fseek
call, the next operation on an update stream may be either input or output.
Returns:
6 The fseek
function returns nonzero only for a request that cannot be satisfied.
我是 C 的新手,正在使用 fseek。我的问题是,它到底有什么作用?
例如:fseek(FILE *stream, -1, SEEK_CUR)
将向后读取 1 个字符,那么它是否已经在文件末尾?
这是否意味着 fseek(FILE *stream, 0, SEEK_CUR)
什么都不做?
我们如何将 fseek
之后的函数应用于数据结构?
I'm new to C and am using
fseek
. My question is, what exactly does it do?
fseek
为 stream
指向的流设置文件位置指示符。新位置将由偏移量结合标志给出:
对于
SEEK_SET
,偏移量是相对于文件的开头。对于
SEEK_CUR
,偏移量是相对于当前指标位置的。对于
SEEK_END
它是相对于文件结尾的。
eg:
fseek(FILE *stream, -1, SEEK_CUR)
will read 1 character backwards, so is it already at the end of the file?
可能在开头,可能在中间,也可能在结尾,当前位置就是现在所在的位置。如果指标位于文件末尾,它将向后移动一个字节。
它不读取字符,如前所述,它将指示器从当前位置偏移到所需位置,如上所述。
Does that mean
fseek(FILE *stream, 0, SEEK_CUR)
does nothing?
实际上它没有做任何可见的事情,因为偏移量为 0 它不移动指标,它只是将它放在相同的位置。它确实清除文件结束状态(重置 EOF
标志
来自 C11 N1570 §7.21.9.2
#include <stdio.h> int fseek(FILE *stream, long int offset, int whence);
Description:
2 The
fseek
function sets the file position indicator for the stream pointed to bystream
. If a read or write error occurs, the error indicator for the stream is set andfseek
fails.
3 For a binary stream, the new position, measured in characters from the beginning of the file, is obtained by adding offset to the position specified bywhence
. The specified position is the beginning of the file ifwhence
isSEEK_SET
, the current value of the file position indicator ifSEEK_CUR
, or end-of-file ifSEEK_END
. A binary stream need not meaningfully support fseek calls with awhence
value ofSEEK_END
.
4 For a text stream, eitheroffset
shall be zero, oroffset
shall be a value returned by an earlier successful call to the ftell function on a stream associated with the same file andwhence
shall beSEEK_SET
.
5 After determining the new position, a successful call to thefseek
function undoes any effects of the ungetc function on the stream, clears the end-of-file indicator for the stream, and then establishes the new position. After a successfulfseek
call, the next operation on an update stream may be either input or output.Returns:
6 The
fseek
function returns nonzero only for a request that cannot be satisfied.