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


fgetpos

Get a stream's current file position indicator value.

Compatibility:

This function is compatible with the following targets:

ANSI

BeOS

EMB/RTOS

Mac OS

Palm OS

Win32


Prototype:
#include <stdio.h>
int fgetpos(FILE *stream, fpos_t *pos);
Parameters:

Parameters for this facility are:

stream  
FILE *  
A pointer to a FILE stream  
pos  
fpos_t  
A pointer to a file position type  

Remarks:

The fgetpos() function is used in conjunction with the fsetpos() function to allow random access to a file. The fgetpos() function gives unreliable results when used with streams associated with a console (stdin, stderr, stdout).

While the fseek() and ftell() functions use long integers to read and set the file position indicator, fgetpos() and fsetpos() use fpos_t values to operate on larger files. The fpos_t type, defined in stdio.h, can hold file position indicator values that do not fit in a long int.

The fgetpos() function stores the current value of the file position indicator for stream in the fpos_t variable pos points to.


NOTE

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

fgetpos() returns zero when successful and returns a nonzero value when it fails.

See Also:

"fseek"

"fsetpos"

"ftell"

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

int main(void)
{
	FILE *f;
	fpos_t pos;
	char filename[80], buf[80];

	// get a filename from the user
	printf("Enter a filename to read.\n");
	gets(filename);

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

	// get the initial file position indicator value
	// (which is at the beginning of the file)
	fgetpos(f, &pos);

	// read each line until end-of-file is reached
	while (fgets(buf, 80, f) != NULL) {
		printf("Once: %s", buf);

		// move to the beginning of the line to read it again
		fsetpos(f, &pos);
		fgets(buf, 80, f);
		printf("Twice: %s", buf);

		// get the file position of the next line
		fgetpos(f, &pos);
	}

	// close the file
	fclose(f);

	return 0;	
}

Output:
Enter a filename to read.
myfoo
Reading each line twice.
Once: chair table chest
Twice: chair table chest
Once: desk raccoon
Twice: desk raccoon			*/


[ 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