Tuesday 24 July 2012

Random Access To File: ftell() | fseek() function

Random Access To File: ftell()

2. ftell()

ftell() function returns the current position of the file pointer. It returns the value after counting from the beginning of the file.

Following program will make its functioning more clear:


/*Program to understand working of ftell() function*/

#include

struct instrument
{
int id;
char name[30];
float price;
}instr;

int main()
{
int n,i;

FILE *fp;
fp=fopen("inst1.dat","wb+");

if(fp==NULL)
{
printf("Error in opening file\n");
return 0;
}

//Accepting number of records for writing
printf("How many records do you want?\n");
scanf("%d",&n);

printf("\n--------Writing in file using fwrite-----------\n\n");
//Writing records one by one in file
for(i=0;i
{
printf("Enter the details of instrument:\n");
printf("Enter Instrument Id :\n");
scanf("%d",&instr.id);
printf("Enter Instrument Name :\n");
scanf("%s",instr.name);
printf("Enter Instrument Price :\n");
scanf("%f",&instr.price);
printf("\n");

//for writing the entire record to the file
fwrite(&instr,sizeof(instr),1,fp);
}



printf("\n\n--------Usage of ftell()-----------\n\n");

fseek(fp,0L,0);
printf("\nPosition of file pointer in the beginning->%d\n",ftell(fp));

while(fread(&instr,sizeof(instr),1,fp))
{
printf("\nPosition of file pointer ->%d\n",ftell(fp));
printf("%d\t",instr.id);
printf("%s\t",instr.name);
printf("%f\t",instr.price);
}

printf("\n Size of file in bytes is %d",ftell(fp));

fclose(fp);

}



Random Access To File: fseek() function

The data stored in the file can be accessed in two ways:

1)Sequentially

2)Randomly


Following functions are used for random access file processing:

-> fseek()

-> ftell()

-> rewind()


1) fseek()


The fseek() function is used to set the position of file pointer at the specified byte.

Declaration:

int fseek(FILE *fp, long displacement, int origin);

where,

fp – file pointer

displacement – it denotes the number of bytes which are skipped. If we want to displace backwards then its value is negative and if we want to displace forward the value is taken as positive.

origin – It is the relative position from where the displacement can take place. We can assign one of the following values to it:








/*Program to understand working of fseek() function*/


#include


struct instrument

{

int id;

char name[30];

float price;

}instr;


int main()

{

int n,i;


FILE *fp;

fp=fopen("inst1.dat","wb+");


if(fp==NULL)

{

printf("Error in opening file\n");

return 0;

}


//Accepting number of records for writing

printf("How many records do you want?\n");

scanf("%d",&n);


printf("\n--------Writing in file using fwrite-----------\n\n");

//Writing records one by one in file

for(i=0;i
{
printf("Enter the details of instrument:\n");
printf("Enter Instrument Id :\n");
scanf("%d",&instr.id);
printf("Enter Instrument Name :\n");
scanf("%s",instr.name);
printf("Enter Instrument Price :\n");
scanf("%f",&instr.price);
printf("\n");

//for writing the entire record to the file
fwrite(&instr,sizeof(instr),1,fp);
}


printf("\n\n--------Reading from file using fseek()-----------\n\n");
printf("Enter the record number to be read:");
scanf("%d",&n);

fseek(fp,(n-1)*sizeof(instr),0);
fread(&instr,sizeof(instr),1,fp);
printf("%d\t",instr.id);
printf("%s\t",instr.name);
printf("%f\t",instr.price);

fclose(fp);

}
Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: