Move the file position indicator.
Compatibility:This function is compatible with the following targets:
|
|
#include <stdio.h>
int fseek(FILE *stream, long offset, int whence);Parameters:
Parameters for this facility are:
The fseek() function moves the file position indicator to allow random access to a file.
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
fseek() returns zero if it is successful and returns a nonzero value if it fails.
#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