Format
#include <stdio.h>
int fseek(FILE *stream, long int offset, int origin);
int fseeko(FILE *stream, off_t offset, int origin);
Language Level: ANSI
Threadsafe: Yes.
Integrated File System Interface: The fseeko() function is not available when SYSIFCOPT(*NOIFSIO) is specified on the compilation command.
Description
The fseek() and fseeko() functions change the current file position that is associated with stream to a new location within the file. The next operation on stream takes place at the new location. On a stream open for update, the next operation can be either a reading or a writing operation.
The fseeko() function is identical to fseek() except that the offset argument is of type off_t.
The origin must be one of the following constants that are defined in <stdio.h>:
For a binary stream, you can also change the position beyond the end of the file. An attempt to position before the beginning of the file causes an error. If successful, the fseek() or fseeko() function clears the end-of-file indicator, even when origin is SEEK_END, and undoes the effect of any preceding the ungetc() function on the same stream.
Return Value
The fseek() or fseeko function returns 0 if it successfully moves the pointer. A nonzero return value indicates an error. On devices that cannot seek, such as terminals and printers, the return value is nonzero.
The value of errno can be set to:
The fseek() and fseeko() functions are not supported for files that are opened with type=record.
Example that uses fseek()
This example opens a file myfile for reading. After performing input operations,fseek() moves the file pointer to the beginning of the file.
#include <stdio.h>
#define MAX_LEN 10
int main(void)
{
FILE *stream;
char buffer[MAX_LEN + 1];
int result;
int i;
char ch;
stream = fopen("mylib/myfile", "r");
for (i = 0; (i < (sizeof(buffer)-1) &&
((ch = fgetc(stream)) != EOF) && (ch != '\n')); i++)
buffer[i] = ch;
result = fseek(stream, 0L, SEEK_SET); /* moves the pointer to the */
/* beginning of the file */
if (result == 0)
printf("Pointer successfully moved to the beginning of the file.\n");
else
printf("Failed moving pointer to the beginning of the file.\n");
}
Related Information