How to Maintain Employees List in .DAT file in C
Alrighty, let’s get crackin’ on this techie treat! Today, we’re diving into the world of C programming, specifically on how to maintain an employee list in a .DAT file. A .DAT file, you ask? That’s our go-to when we need a file format that’s simple, efficient, and as straightforward as my grandma’s recipe for chai! 😄
Getting the Basics Right: Why C and .DAT Files?
First off, why are we even talking about C and .DAT files? C programming is like the granddaddy of modern languages. It’s been around since the disco era and still rocks it in system programming, embedded systems, and yup, handling data files.
And .DAT files? They’re the chameleons of the file world. Not tied down to any specific format, these files can store text, binary data, or a mix of both. They’re perfect for our use-case: keeping a list of employees where we can store details like names, IDs, and roles.
Setting Up Our Stage: The Employee List Structure
Before we jump into code, let’s sketch out what our employee list is gonna look like. We’re talking about a structure here, folks. In C, a structure is like a custom datatype where you can store different types of data. It’s like a mixed bag of goodies!
typedef struct {
int id;
char name[50];
char role[30];
} Employee;
Here, each employee has an ID, a name, and a role. Simple, right? The typedef
makes our life easier by letting us use Employee
as a type, just like int
or char
.
The Nuts and Bolts: Writing to a .DAT File
Now, let’s get to the fun part – writing our employee data to a .DAT file. In C, this means playing with file operations. We’re gonna use fopen()
, fwrite()
, and fclose()
. It’s like opening a diary, writing in it, and then locking it up.
void writeEmployeeData(Employee emp) {
FILE *file = fopen("employees.dat", "ab");
if (file == NULL) {
printf("Error! Can't open file.\n");
exit(1);
}
fwrite(&emp, sizeof(Employee), 1, file);
fclose(file);
}
In this snippet, fopen()
opens our .DAT file in append-binary mode (‘ab’). Why binary? It’s efficient and doesn’t mess with our data. The fwrite()
function then does the heavy lifting of writing our employee structure to the file.
Reading from the File: Where the Magic Happens
What good is writing data if you can’t read it back, right? To read from our .DAT file, we’ll use fopen()
, fread()
, and yup, fclose()
again. It’s like opening that diary back up to relive the memories.
void readEmployeeData() {
Employee emp;
FILE *file = fopen("employees.dat", "rb");
if (file == NULL) {
printf("Error! Can't open file.\n");
exit(1);
}
while(fread(&emp, sizeof(Employee), 1, file)) {
printf("ID: %d, Name: %s, Role: %s\n", emp.id, emp.name, emp.role);
}
fclose(file);
}
Here, fopen()
opens the file in read-binary mode (‘rb’). We then loop through the file with fread()
, reading one Employee
structure at a time until we hit the end of the file.
Handling Updates: Because Change is Inevitable
Updating a record in a .DAT file requires a bit more finesse. You gotta find the record, update it, and write it back. It’s like finding a needle in a haystack, except the needle is your employee data, and the haystack is… well, still a haystack. 😅
void updateEmployeeData(int id, Employee newEmpData) {
Employee emp;
FILE *file = fopen("employees.dat", "r+b");
if (file == NULL) {
printf("Error! Can't open file.\n");
exit(1);
}
while(fread(&emp, sizeof(Employee), 1, file)) {
if(emp.id == id) {
fseek(file, -sizeof(Employee), SEEK_CUR);
fwrite(&newEmpData, sizeof(Employee), 1, file);
break;
}
}
fclose(file);
}
In this spicy piece of code, we open the file in read-write binary mode (‘r+b’). We then loop through the file, find the employee with the matching ID, and use fseek()
to move the file pointer back to the start of that record. Then, fwrite()
updates it with the new data. Bam!
Deleting Records: When It’s Time to Say Goodbye
Deleting a record is a bit tricky. Since .DAT files don’t have a delete function per se, we gotta get creative. We’ll read all the data except the one we want to delete and write it to a temporary file. Then, we replace the original file with this temporary one. It’s like a magic trick, but with files.
void deleteEmployeeData(int id) {
Employee emp;
FILE *file = fopen("employees.dat", "rb");
FILE *tempFile = fopen("temp.dat", "wb");
if (file == NULL || tempFile == NULL) {
printf("Error! Can't open file.\n");
exit(1);
}
while(fread(&emp, sizeof(Employee), 1, file)) {
if(emp.id != id) {
fwrite(&emp, sizeof(Employee), 1, tempFile);
}
}
fclose(file);
fclose(tempFile);
remove("employees.dat");
rename("temp.dat", "employees.dat");
}
Here, we read from the original file and write to temp.dat
all records except the one we want to delete. Then, we just delete the old file and rename the temp file. Voilà!
Reflection: Why This Matters
So, why go through all this trouble? Managing data in C using .DAT files is like learning to make the perfect biryani. It takes patience, practice, and a bit of trial and error, but once you get it, it’s oh-so-satisfying. Plus, understanding file handling in C gives you a solid foundation in programming basics, which is always a good thing.
Overall, playing with data in C using .DAT files is a fantastic way to sharpen your programming skills. It might seem a bit old school, but hey, classics are classics for a reason! And who knows, these skills might just come in handy when you least expect it. Thanks for hanging out with me on this coding adventure! Stay curious, keep coding, and catch you on the flip side! ✌️
In this program, I am saving Employee data into a .dat file. When program starts, it ask the user how many employee the user want to save into file. After entering the employee number, then I used for loop to get employee number, name and basic pay, allowances & deductions. After getting the information about Employee, I saved them in a employee.dat file and then read all data from that file and show in screen.
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fptr ;
int i, n, empno ;
float bpay, allow, ded ;
char name[10] ;
fptr = fopen("EMPLOYEE.DAT", "w") ;
printf("Enter the number of employees : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("Enter the employee number : ") ;
scanf("%d", &empno) ;
printf("Enter the name : ") ;
scanf("%s", name) ;
printf("Enter the basic pay, allowances & deductions : ") ;
scanf("%f %f %f", &bpay, &allow, &ded) ;
fprintf(fptr, "%d %s %f %f %f", empno, name, bpay, allow, ded) ;
}
fclose(fptr) ;
fptr = fopen("EMPLOYEE.DAT", "r") ;
printf("Emp. No. Name Bpay Allow Ded Npay\n");
for(i = 0 ; i < n ; i++)
{
fscanf(fptr,"%d %s %f %f %f", &empno, name, &bpay, &allow, &ded) ;
printf("%d %s %.2f %.2f %.2f %.2f\n", empno, name, bpay, allow, ded, bpay + allow - ded) ;
}
fclose(fptr);
return 0;
}