Reputation: 13
I'm fairly new to C. At the moment, I'm learning about file management. To further my understanding of files I wanted to create a Employee Database System, where the user can Create, Delete, Update or Retrieve an Employee Record. First I created a struct which contains the employee's variables like ID, first name, last name and pay, then a function which creates the records, but the there is the problem. I know how to search from the file (using the ID number) but I don't know how to delete a particular record.
(I'm so sorry because I accidentally deleted my DeleteFunction so I cannot show that particular function in my code)
So here is my code:
#include <stdio.h>
#include <string.h>
typedef struct employee{
int id;
char name[40];
float pay;
}EMP;
void CreateEmployee(struct employee [] );
void DisplayRecord();
int main() {
EMP e[20];
CreateEmployee(e);
//return 0;
DisplayRecord();
return 0;
}
void CreateEmployee(struct employee x[]){
char choice ='y';
int i=0;
//EMP employee[20];
FILE *fp;
fp = fopen("employee.txt","a");
if (fp==NULL){
printf("File not created");
}
while((choice == 'Y') || (choice =='y')){
printf("Enter the employee's id:");
scanf("%i",&x[i].id);
fprintf(fp,"%i\t",x[i].id);
printf("\nEnter the employee's name:");
scanf("%s",x[i].name);
fprintf(fp,"%s\t",x[i].name);
printf("\nEnter the employee's pay:");
scanf("%f",&x[i].pay);
fprintf(fp,"%.2f\t\n",x[i].pay);
printf("\nEnter another employee?\n");
printf("Y - Yes N - No:\n");
scanf("\n%c",&choice);
i++;
}
fclose(fp);
}
void DisplayRecord(){
EMP temp;
FILE *fp = fopen("employee.txt", "r");
if(fp != NULL)
{
while( !feof(fp) )
{
fscanf(fp, "%i %s %f", &temp.id, temp.name, &temp.pay);
printf("%i %s %f\n",temp.id, temp.name, temp.pay);
}
fclose(fp);
}
else
{
printf("An error occurred opening the file\n");
}
}
I don't know how to use random access file as yet; that's why I'm using sequential access at the moment.
Upvotes: 1
Views: 7139
Reputation: 41625
To delete a record, you have several choices:
Prepare by having a deleted
field in each record, and when reading records, skip those that have deleted == true
. To actually delete a record, set its deleted
flag to true
and save it.
Read the data file and write all records to a temporary file, except the one you want to delete. Finally, rename the temporary file to employees.txt
.
Switch to a real database for managing the records, since all the typical operations are fast and easy then.
By the way, you haven't said why you love C as a programming language. If it is because you can quickly get your things done, you will be disappointed as soon as your programs get a little larger, since you have to do the memory management and error handling explicitly in your code. And, C makes it very easy to shoot you in the foot (so your program crashes and just doesn't work), since it doesn't have built-in memory protection. Therefore, before totally falling in love with that language, look around a little bit too see whether you can find something better.
Upvotes: 2