-->
Showing posts with label program to understand ftell() function. Show all posts
Showing posts with label program to understand ftell() function. Show all posts

program to understand ftell() function

using codeblocks
------------------------------------------------------------------
/*program to 'struct ' concept to store employee id,first name and salary to a data file until you say 'n'.
Then display all those stored records.
*/
#include <stdio.h>
#include<conio.h>
struct employee                           //struct tag
{
    int employee_id;
    float employee_salary;
    char employee_name[50];
}var;                                      //variable
int main()
{
FILE *k;                                  //  pointer for file declaration
char choice;                              //identifier declaration
k=fopen("employees.txt","w");             //file opening in write mode
do
{
printf("\n enter employee id\n");
scanf("%d",&var.employee_id);
printf("enter employee salary\n");
scanf("%f",&var.employee_salary);
printf("enter  employee name\n");       // getting inputs
scanf("%s",var.employee_name);
fwrite(&var,sizeof(var),1,k);; //writing data to data file
printf("want to continue (y/n):=");//prints message
choice=getche();                      //gets a character
}while(choice!='n');        //writing repeats as you enter 'y'
printf("\n writing process completed successfully\n");
rewind(k);
fclose(k);                            //closing of file                                                            //closing data file
printf("-----------------------\n");
printf("reading data\n");             //opening file in read mode
k=fopen("employees.txt","r");               //file opening

while((fread(&var,sizeof(var),1,k))==1)// fread() reads the records till it is true.
                                        //As it becomes less than 1, it stops
{
    printf("employee id=%d,employee salary=%f,employee name=%s\n",var.employee_id,var.employee_salary,var.employee_name);//prints the struct value
}
//location=ftell(k);
printf("the location using ftell=%ld\n",ftell(k));// tells us the location of pointer.
fseek(k,2,SEEK_SET);                               // sets the pointer to second position
printf("the location after using seek_set=%ld\n",ftell(k));
fseek(k,0,SEEK_CUR);                               //takes the current location of pointer.
                                                  //IF we take any other value than 0 ,
                                                   //it adds that to previous location and displays that
printf("the location after using seek_cur=%ld\n",ftell(k));
fseek(k,0,SEEK_END);                               //takes  position of pointer is at the end.
printf("the location after using seek_end=%ld\n",ftell(k));
rewind(k);                                         //takes the pointer to 0 location i.e. beginning
printf("the location=%ld\n",ftell(k));
fclose(k);                            //closing of file.
getch();
return 0;
}
----------------------------------------------------------------------------------------------------------
using turbo c++
-----------------------------------------------------------
/*program to 'struct ' concept to store employee id,first name and salary to a data file until you say 'n'.
Then display all those stored records.
*/
#include <stdio.h>
#include<conio.h>
struct employee                           //struct tag
{
    int employee_id;
    float employee_salary;
    char employee_name[50];
}var;                                      //variable
int main()
{
FILE *k;                                  //  pointer for file declaration
char choice;                              //identifier declaration
k=fopen("employees.txt","w");             //file opening in write mode
do
{
printf("\n enter employee id\n");
scanf("%d",&var.employee_id);
printf("enter employee salary\n");
scanf("%f",&var.employee_salary);
printf("enter  employee name\n");       // getting inputs
scanf("%s",var.employee_name);
fwrite(&var,sizeof(var),1,k);; //writing data to data file
printf("want to continue (y/n):=");//prints message
choice=getche();                      //gets a character
}while(choice!='n');        //writing repeats as you enter 'y'
printf("\n writing process completed successfully\n");
rewind(k);
fclose(k);                            //closing of file                                                            //closing data file
printf("-----------------------\n");
printf("reading data\n");             //opening file in read mode
k=fopen("employees.txt","r");               //file opening

while((fread(&var,sizeof(var),1,k))==1)// fread() reads the records till it is true.
                                        //As it becomes less than 1, it stops
{
    printf("employee id=%d,employee salary=%f,employee name=%s\n",var.employee_id,var.employee_salary,var.employee_name);//prints the struct value
}
//location=ftell(k);
printf("the location using ftell=%ld\n",ftell(k));// tells us the location of pointer.
fseek(k,2,SEEK_SET);                               // sets the pointer to second position
printf("the location after using seek_set=%ld\n",ftell(k));
fseek(k,0,SEEK_CUR);                               //takes the current location of pointer.
                                                  //IF we take any other value than 0 ,
                                                   //it adds that to previous location and displays that
printf("the location after using seek_cur=%ld\n",ftell(k));
fseek(k,0,SEEK_END);                               //takes  position of pointer is at the end.
printf("the location after using seek_end=%ld\n",ftell(k));
rewind(k);                                         //takes the pointer to 0 location i.e. beginning
printf("the location=%ld\n",ftell(k));
fclose(k);                            //closing of file.
getch();
return 0;
}