Code:
/*dat_lib.c file*/
#include"dat_lib.h"
void add_record(FILE *fp){
another = 'Y';
fseek(fp,0,SEEK_END);
while(another == 'Y' || another == 'y')
{
printf("::::::: Record Adding Section :::::::\n");
printf("Enter name, age and basic salary: \n");
scanf("%s%d%f", e.name, &e.age, &e.bs);
fwrite(&e,sizeof(e),1,fp);
printf("Add more records (Y/N)\n");
fflush(stdin); /* if you don't use it, next statement will be skipped
because already a character in buffer.*/
scanf("%c", &another);
}
}
void modify_record(FILE *fp){
another = 'Y';
while(another == 'Y' || another == 'y')
{
printf("::::::: Record Modifying Section :::::::\n");
printf("\nEnter name of employee to modify record!\n");
scanf("%s", empname);
rewind(fp);
//fseek(fp,0,SEEK_SET); it has same output as rewind() function, so is alernate of rewind() function.
while(fread(&e,sizeof(e),1,fp) == 1)
{
if(strcmp(e.name,empname) == 0) // [Error detected] use if, not while
{
fseek(fp,-recsize,SEEK_CUR); /* [Error detected] -1 byte is wrong we need backward record
so we used -recsize. -recsize because fread has incremented next already in buffer*/
printf("Enter new name, age, basic salary!\n");
scanf("%s %d %f", e.name, &e.age, &e.bs);
fwrite(&e,sizeof(e),1,fp);
break; // [Error detected] added it
}
}
printf("Do you want to modify more record/s (Y/N)!\n");
fflush(stdin);
another = getche();
}
}
void delete_record(FILE *fp, FILE *ft){
another = 'Y';
while(another == 'Y' || another == 'y')
{
rewind(fp);
ft = fopen("temp.dat", "wb"); /* required only to write content from fp to ft (temporary file)
and no read required i.e. by fread() so is used wb.*/
printf("::::::: Record Deletion Section :::::::\n");
printf("Enter name of employee to delete record!\n");
scanf("%s", empname); // empname is address of first element and &empname of first row.
//printf("%s", empname); this statement can be used to know that what content is in empname.
while(fread(&e,sizeof(e),1,fp) == 1)
{
/*printf("fread = %d\n", fread(&e,sizeof(e),1,fp)); making trouble,
it again reading next record and now putting next record in e
so first record was being missed to copy from fp to ft.
Result can be checked by using and executing this statement.*/
if(strcmp(e.name,empname) != 0) // here while was making trouble
{
printf("strcmp = %d\n", strcmp(e.name,empname)); /*it will print only non zeros
because condition be true in if-statement only by being non zero and thus control
comes in. If we want to print zeros too, we can use this printf statement
immediate after if-block of stamements.*/
/*fseek(fp,-sizeof(e),SEEK_CUR); sizeof(e), here sizeof() operator returns unsigned constant int.
Here we are using -ve sign with it. That makes trouble. Why? Yet I don't know exactly.
Further here no need to reverse file pointer because record is always at this point i.e. in struct emp e*/
fwrite(&e,sizeof(e),1,ft); /* fread() in above statement read from one file and record to stuct emp e.
Then fwrite take record from struct emp e and write it on another file. This is a way to read or write or coping
from one file to another file.*/
}
/*fclose(fp);
fclose(ft);
remove("emp.dat"); these statements were making problem here.
rename("temp.dat","emp.dat");
fp = fopen("emp.dat","wb+"); [Error detected] Opened in wb+ here is deleteing all content.
So,rb+ is right choice.*/
}
fclose(fp);
fclose(ft); // without close a file, can't be renamed nor deleted.
remove("emp.dat");
rename("temp.dat","emp.dat");
fp = fopen("emp.dat","rb+"); /* File is opened again, so that
if required to perform more operations on file, could be performed.*/
printf("Do you want to delete more record/s (Y/N)!\n");
fflush(stdin);
another = getche();
}
}
void display_record(FILE *fp){
long int position;
position = ftell(fp); /*ftell() function is used to find out the position of file pointer
in the file with respect to starting of the file. It returns long int i.e. 4 bytes (if int 2 bytes)
or 8 bytes (if int 4 bytes)*/
printf("::::::: Record Displaying Section :::::::\n");
printf("Position b/rewind: %ld\n", position);
rewind(fp);
position = ftell(fp);
printf("Position a/rewind: %ld\n", position);
//fseek(fp,0,SEEK_SET);
printf("%-15s%-5s%-10s\n", "Name", "Age", "Basic Salary"); // Instead of variables, we provided string constants.
while(fread(&e, sizeof(e), 1, fp) == 1)
printf("%-15s%-5d%-10.2f\n", e.name, e.age, e.bs);
system("pause");
}
void query_record(FILE *fp){
char choice;
rewind(fp);
printf("::::::: Query Section :::::::\n");
printf("1. Query by Name:\n");
printf("2. Query by age:\n");
printf("3. Query by Basic Salary:\n");
printf("Entre your choice:\n");
choice = getche();
printf("\n");
switch(choice)
{
case '1':
query_name(fp);
break;
case '2':
query_age(fp);
break;
case '3':
query_bs(fp);
break;
default:
printf("Entre correct choice:\n");
}
system("pause");
} // query_record(FILE *fp)
void query_name(FILE *fp)
{
char name[40];
printf("Query record by name:\n");
printf("Enter name for Query: ");
scanf("%s", name);
while(fread(&e, sizeof(e), 1, fp) == 1)
{
if(strcmpi(e.name,name) == 0)
printf("%s %d %0.2f\n", e.name, e.age, e.bs);
}
}
void query_age(FILE *fp)
{
int age;
char choice;
printf("Query record by age:\n");
printf("Enter age for Query: ");
scanf("%d", &age);
printf("\n");
printf("Please select from the following:\n");
printf("1. greater than %d\n", age);
printf("2. less than %d\n", age);
printf("3. equal to %d\n", age);
printf("Enter your choice:\n");
fflush(stdin);
choice = getche();
printf("\n");
//scanf("%c", &choice);
switch(choice)
{
case '1': /* here 1 is a character because in single quote.
We can take 1 as integer as well.*/
while(fread(&e, sizeof(e), 1, fp) == 1)
{
if(e.age > age)
{
printf("%s %d %0.2f\n", e.name, e.age, e.bs);
}
}
break;
case '2':
while(fread(&e, sizeof(e), 1, fp) == 1)
{
if(e.age < age)
{
printf("%s %d %0.2f\n", e.name, e.age, e.bs);
}
}
break;
case '3':
while(fread(&e, sizeof(e), 1, fp) == 1)
{
if(e.age == age)
{
printf("%s %d %0.2f\n", e.name, e.age, e.bs);
}
}
break;
default:
printf("Enter correct choice:\n");
}
}
void query_bs(FILE *fp)
{
char choice;
float bs;
printf("Query record by basic salary:\n");
printf("Enter basic salary for query: ");
scanf("%f", &bs);
printf("\n");
printf("Please select from the following:\n");
printf("1. greater than %0.2f\n", bs);
printf("2. less than %0.2f\n", bs);
printf("3. equal to %0.2f\n", bs);
printf("Enter your choice:\n");
fflush(stdin);
choice = getche();
printf("\n");
//scanf("%c", &choice);
switch(choice)
{
case '1': /* here 1 is a character because in single quote.
We can take 1 as integer as well.*/
while(fread(&e, sizeof(e), 1, fp) == 1)
{
if(e.bs > bs)
{
printf("%s %d %0.2f\n", e.name, e.age, e.bs);
}
}
break;
case '2':
while(fread(&e, sizeof(e), 1, fp) == 1)
{
if(e.bs < bs)
{
printf("%s %d %0.2f\n", e.name, e.age, e.bs);
}
}
break;
case '3':
while(fread(&e, sizeof(e), 1, fp) == 1)
{
if(e.bs == bs)
{
printf("%s %d %0.2f\n", e.name, e.age, e.bs);
}
}
break;
default:
printf("Enter correct choice:\n");
}
}