Write a menu driven program to create a binary file, read all contents of the file, search a particular record in the file. (in python)
Answers
Here is a sample menu-driven program in Python that can be used to create a binary file, read the contents of the file, and search for a particular record in the file:
Create a binary file:
- First, open a new file in binary write mode using the open function.
- Then, use the write method to write the data to the file.
- Close the file using the close method.
- Read the contents of the file:
- Open the file in binary read mode using the open function.
- Use the read method to read the contents of the file.
- Close the file using the close method.
- Search for a particular record in the file:
- Open the file in binary read mode using the open function.
- Use a loop to iterate through each record in the file.
- Within the loop, use an if statement to check if the current record matches the search criteria.
- If a match is found, print the record and break out of the loop.
- Close the file using the close method.
Here is the sample code for the menu-driven program:
import struct
def create_binary_file():
# Open a new file in binary write mode
with open('binary_file.bin', 'wb') as file:
# Write some data to the file
file.write(struct.pack('i', 123))
file.write(struct.pack('f', 123.456))
file.write(struct.pack('c', b'a'))
def read_binary_file():
# Open the file in binary read mode
with open('binary_file.bin', 'rb') as file:
# Read the contents of the file
data = file.read()
print(data)
def search_binary_file(search_value):
# Open the file in binary read mode
with open('binary_file.bin', 'rb') as file:
# Iterate through each record in the file
while True:
# Read a single record from the file
record = file.read(4)
# If we have reached the end of the file, break out of the loop
if not record:
break
# Unpack the record
value = struct.unpack('i', record)[0]
# Check if the value matches the search criteria
if value == search_value:
print(f'Match found: {value}')
break
# Main program
while True:
print('1. Create binary file')
print('2. Read binary file')
print('3. Search binary file')
print('4. Exit')
choice = int(input('Enter your choice: '))
if choice == 1:
create_binary_file()
elif choice == 2:
read_binary_file()
elif choice == 3:
search_value = int(input('Enter the value to search for: '))
search_binary_file(search_value)
elif choice == 4:
break
else:
print('Invalid choice')
To learn more about binary file from the given link.
https://brainly.in/question/19625904
#SPJ1
Answer:
here are the direct codes for your question,it may show some errors in your pc due to different version if you have the updated version you will have no problem
planation:
im port pickle as pk
import sys
def add():
f=open("result.dat","ab")
record=[]
while True:
roll_no=int(input("enter the roll no:"))
name=input("enter the name:")
Class=int(input("enter the class:"))
tot_marks=int(input("enter the tot_marks:"))
data=[roll_no,name,Class,tot_marks]
record.append(data)
ch=input("do you want to add more data(y/n):")
if ch in 'Nn':
break
pk.dump(record,f)
print("record added")
f.close()
def display():
f=open("result.dat","rb")
try:
while True:
s=pk.load(f)
print(s)
for i in s:
print(i)
except EOFError:
f.close()
def search():
f=("result.dat","rb")
r=int(input("enter the rollno which you want to enter:"))
found=0
try:
while True:
s=pk.load(f)
for i in s:
if i[0]==r:
print(i)
found=1
break
except EOFError:
f.close()
if found==0:
print("no record found")
def exitt():
sys.exit
def main_menu():
print("1. ADD RECORDS")
print("2. DISPLAY RECORDS")
print("3. SEARCH RECORDS")
print("4. EXIT")
while True:
ch=int(input("enter your choice(1-4):"))
if ch==1:
add()
elif ch==2:
display()
elif ch==3:
search()
elif ch==4:
exitt()