Direct Access Binary File Operations with Struct Library | A Level | By ZAK

Опубликовано: 14 Октябрь 2024
на канале: Zafar Ali Khan (ZAK)
203
6

Resource created in video: https://bit.ly/direct-access-binary-f...

NB: You must have a promising idea of Python Struct module before watching this lecture.

Direct Access Binary File Operations with Struct Library: This code is designed to manage a binary direct access file containing employee records. It uses the Struct library to read and write binary data. The file contains three fields for each employee: employee id (integer), employee name (string), and employee salary (float).

The file is created with 1000 records, with each record having 28 bytes: 4 bytes for employee id, 20 bytes for employee name, and 4 bytes for employee salary. To store and retrieve the record, a modulo hashing function (hash_value = key % 1000) is used, and in case of collisions, linear probing is employed.

The code performs the following operations on the file:
1. Create the file
2. Insert a record
3. Search for a record
4. Update a record
5. Delete a record
6. Display all records

Here's a brief explanation of each function:
1. create_file(): Creates a new binary file with 1000 records, each record initialized with default values (0 for id, an empty string for name, and 0.0 for salary).

2. insert_record(): Inserts a new record by getting user input for employee id, name, and salary. It calculates the hash value using the hash function and checks if the record is already present in the file. If not, the new record is inserted; otherwise, it prints "Record already exists."

3. search_record(): Searches for a record by getting the employee id from the user. It calculates the hash value using the hash function and looks for the record in the file. If found, it prints the record; otherwise, it prints "Record does not exist."

4. update_record(): Updates a record by getting the employee id from the user. It calculates the hash value using the hash function, searches for the record in the file, and if found, prompts the user to enter the new employee's name and salary. The record is then updated in the file.

5. delete_record(): Deletes a record by getting the employee id from the user. It calculates the hash value using the hash function, searches for the record in the file, and if found, asks for confirmation to delete the record. If the user confirms, the record is deleted.

6. display_all_records(): Displays all records in the file by reading and unpacking the records, then printing them.

Overall, this code provides a simple yet powerful way to manage employee records in a binary direct access file using Python's Struct library.


INTRODUCTION:
Zafar Ali Khan "ZAK" is an A and O level Computer Science Pakistani educator, entrepreneur, and former programmer. He supports a free online education platform to impart O and A level resources. His resources include notes, video lectures, online discussion groups, and a full website.

ZAK is teaching a wide spectrum of learners for his subjects, focusing on Computer Science 2210, 9608 and Computing 9691. ZAK has a strong presence on social media and has approx 50000+ followers.

Website: http://www.zakonweb.com
Workshops: http://www.zakathon.com
Phone App: https://bit.ly/ZakApp

Online Discussions Group:
OL http://www.zakonweb.com/ogroup
AL http://www.zakonweb.com/agroup
YouTube: http://www.zakonweb.com/videos
Github: http://www.github.com/zakonweb
Blog: http://www.zakonweb.com/blog
Facebook: /olevelcomputer, /alevelcomputer
Email: [email protected]
WhatsApp/Call: +923 111 222 ZAK (925)