0% found this document useful (0 votes)
2 views

Binary_File_Program

The document describes a menu-driven program for managing employee records in a binary file named 'Employee.dat'. It allows users to add records, display all records, search for a specific record by employee ID, and exit the program. The employee record structure includes fields for Employee ID, Name, Department, and Salary.

Uploaded by

Harmeet Dodia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Binary_File_Program

The document describes a menu-driven program for managing employee records in a binary file named 'Employee.dat'. It allows users to add records, display all records, search for a specific record by employee ID, and exit the program. The employee record structure includes fields for Employee ID, Name, Department, and Salary.

Uploaded by

Harmeet Dodia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

'''

Write a menu driven program to perform following operations into a binary file
‘Employee.dat’.
1. Add record
2. Display all records
3. Search record
4. Exit
The structure of file content is: [ Empid, EName, Dept, Salary ]
'''

import pickle

def add_rec():
f=open("Employee.dat", 'ab')
empid = input("Employee ID: ")
ename = input("Employee Name: ")
dept = input("Department: ")
sal = input("Salary: ")
record = [empid, ename, dept, sal]
pickle.dump(record, f)
print("Record added successfully...")

def disp():
try:
f=open("Employee.dat", 'rb')
print("\nAll Employee Records:")
while True:
try:
data = pickle.load(f)
print("Employee ID:", data[0])
print("Employee Name:", data[1])
print("Department:", data[2])
print("Salary:", data[3])
print("=" * 30)
except EOFError:
break
except FileNotFoundError:
print("No records found.")

def search():
empid_to_search = input("Enter Employee ID to search: ")
try:
f=open("Employee.dat", 'rb')
while True:
try:
data = pickle.load(f)
if data[0] == empid_to_search:
print("Employee ID:", data[0])
print("Employee Name:", data[1])
print("Department:", data[2])
print("Salary:", data[3])
print("=" * 30)
break
except EOFError:
print("Data not found...!")
break
except FileNotFoundError:
print("No records found.")

# Main loop for the menu


while True:
print('''
1. Add record
2. Display all records
3. Search record
4. Exit
''')
ch = int(input("Choose from above options: "))
if ch == 1:
add_rec()
elif ch == 2:
disp()
elif ch == 3:
search()
elif ch == 4:
print("Exiting the program.")
break
else:
print("Invalid option. Please try again.")

You might also like