codescracker


c

C fseek()



« Previous Function Next Function »


fseek()

The fseek() function is used to set the file position indicator associated with stream according to the values of offset and origin. The purpose of it is to support the random access I/O operations. The offset is the number of bytes from the origin to seek to. The values for origin must be one of the following macros defined in <stdio.h>

C Programming Table:
Name Meaning
SEEK_SET Seek from the start of file
SEEK_CUR Seek from the current location
SEEK_END Seek from the end of file

fseek() Syntax

#include<stdio.h>
int fseek(FILE *stream, long int offset, int origin);

fseek() returns zero on success, otherwise non-zero on failure

fseek() Example

Following function seeks to the specified structure of type addr

Notice here the use of sizeof to obtain the size of structure:

struct addr
{
	char name[40];
	char street[40];
	char city[40];
	char state[3];
	char zip[10];
}info;

void find(long int client_num)
{
	FILE *fp;
	fp=fopen("mail", "rb");
	if(fp==NULL)
	{
		printf("Error in opening the file..!!\n");
		printf("Press any key to exit..\n");
		getch();
		exit(1);
	}
	
	/* find proper structure */
	fseek(fp, client_num*sizeof(struct addr), SEEK_SET);
	
	/* read data into memory */
	fseek(&info, sizeof(struct addr), 1, fp);
	fclose(fp);
}

« Previous Function Next Function »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test