fseek
Go Up to stdio.h Index
Header File
stdio.h
Category
Input/output Routines
Prototype
int fseek(FILE *stream, long offset, int whence); int _fseeki64(FILE *stream, __int64 offset, int whence);
Description
Repositions a file pointer on a stream.
fseek sets the file pointer associated with the stream to a new position that is offset bytes from the file location given by whence. For text mode streams offset should be 0
or a value returned by ftell.
whence must be one of the values 0
, 1
, or 2
, which represent three symbolic constants (defined in stdio.h) as follows:
|
|
File beginning
|
|
|
Current file pointer position
|
|
|
End-of-file
|
fseek discards any character pushed back using ungetc. fseek is used with stream I/O; for file handle I/O, use lseek.
After fseek, the next operation on an update file can be either input or output.
Return Value
fseek returns 0
if the pointer is successfully moved and nonzero on failure.
In the event of an error return, the global variable errno is set to one of the following values:
|
Bad file pointer |
|
Invalid argument |
|
Illegal seek on device |
Example
#include <stdio.h> long filesize(FILE *stream); int main(void) { FILE *stream; stream = fopen("MYFILE.TXT", "w+"); fprintf(stream, "This is a test"); printf("Filesize of MYFILE.TXT is %ld bytes\n", filesize(stream)); fclose(stream); return 0; } long filesize(FILE *stream) { long curpos, length; curpos = ftell(stream); fseek(stream, 0L, SEEK_END); length = ftell(stream); fseek(stream, curpos, SEEK_SET); return length; }
Portability
POSIX | Win32 | ANSI C | ANSI C++ |
---|---|---|---|
+ |
+ |
+ |
+ |