Header File
stdio.h
Category
Input/output Routines
Prototype
int fseek(FILE *stream, long offset, int whence);
Description
Repositions a file pointer on a stream.
fseek sets the file pointer associated with 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:
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.
fseek might return a 0 indicating that the pointer has been moved successfully when in fact it has not been. This is because DOS, which actually resets the pointer, does not verify the setting. fseek returns an error code only on an unopened file or device.
In the event of an error return the global variable errno is set to one of the following values:
EBADF |
Bad file pointer |
EINVAL |
Invalid argument |
ESPIPE |
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++ |
+ |
+ |
+ |
+ |
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|