[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


fseek

Move the file position indicator.

Compatibility:

This function is compatible with the following targets:

ANSI

BeOS

EMB/RTOS

Mac OS

Palm OS

Win32


Prototype:
#include <stdio.h>
int fseek(FILE *stream, long offset, int whence);
Parameters:

Parameters for this facility are:

stream  
FILE *  
A pointer to a FILE stream  
offset  
long  
The offset to move in bytes  
whence  
int  
The starting position of the offset  

Remarks:

The fseek() function moves the file position indicator to allow random access to a file.

The function moves the file position indicator either absolutely or relatively. The whence argument can be one of three values defined in stdio.h: SEEK_SET, SEEK_CUR, SEEK_END.

The SEEK_SET value causes the file position indicator to be set offset bytes from the beginning of the file. In this case offset must be equal or greater than zero.

The SEEK_CUR value causes the file position indicator to be set offset bytes from its current position. The offset argument can be a negative or positive value.

The SEEK_END value causes the file position indicator to be set offset bytes from the end of the file. The offset argument must be equal or less than zero.

The fseek() function undoes the last ungetc() call and clears the end-of-file status of stream.


NOTE

The function fseek has limited use when used with MS DOS text files opened in text mode because of the carriage return / line feed translations. For more information review "Text Streams and Binary Streams."

The fseek operations may be incorrect near the end of the file due to eof translations.

The only fseek operations guaranteed to work in MS DOS text files opened in text mode are:

Using the offset returned from ftell() and seeking from the beginning of the file.

Seeking with an offset of zero from SEEK_SET, SEEK_CUR and SEEK_END.


NOTE

On embedded/ RTOS systems this function only is implemented for stdin, stdout and stderr files.
Return:

fseek() returns zero if it is successful and returns a nonzero value if it fails.

See Also:

"fgetpos"

"fsetpos"

"ftell"

Example of fseek() usage.:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	FILE *f;
	long int pos1, pos2, newpos;
	char filename[80], buf[80];
	
	// get a filename from the user
	printf("Enter a filename to read.\n");
	gets(filename);

	// open a file for input
	if (( f = fopen(filename, "r")) == NULL) {
		printf("Can't open %s.\n", filename);
		exit(1);
	}

	printf("Reading last half of first line.\n");

	// get the file position indicator before and after
	// reading the first line
	pos1 = ftell(f);	
	fgets(buf, 80, f);
	pos2 = ftell(f);
	printf("Whole line: %s\n", buf);

	// calculate the middle of the line
	newpos = (pos2 - pos1) / 2;

	fseek(f, newpos, SEEK_SET);
	fgets(buf, 80, f);
	printf("Last half: %s\n", buf);

	// close the file
	fclose(f);

	return 0;
}

Output:
Enter a filename to read.
itwerks
Reading last half of first line.
Whole line: The quick brown fox

Last half: brown fox


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 16, 2000