Create a binary file with name and roll number. Search for a given roll number and display the name, if not found display appropriate message -Pracrtical 083 CS XII

Create a binary file with name and roll number. Search for a given roll number and display the name, if not found display appropriate message.

Here I have mentioned the Sample Output & Source code of  a binary file with name and roll number. Search for a given roll number and display the name, if not found display appropriate message.

Create a binary file with name and roll number


Program:

Aim: 

To Create a binary file with name and roll number. Search for a given roll number and display the name, if not found display appropriate message.

Procedure: 

Step1: Here is the first process of the program to import the module for Binary file which we call its as "pickle" & sys.

Create a binary file with name and roll number

Step 2:  Here is the second process of the program to define display() function.

Create a binary file with name and roll number


Step 3:  Here is the third process of the program to define search() for search data from binary file. 

Create a binary file with name and roll number

Step 4:  Here is the fourth process of the program to define main program.

Create a binary file with name and roll number

Sample Output: 

Create a binary file with name and roll number

Source Code:

#Practical Program Create a binary file with name and roll number.
#Search for a given roll number and display the name, if not found
#display appropriate message.


import pickle
import sys
dict={}
def write_in_file():
    file=open("D:\\stud2.dat","ab")  #a-append,b-binary
    no=int(input("ENTER NO OF STUDENTS: "))
    for i in range(no):
        print("Enter details of student ", i+1)
        dict["roll"]=int(input("Enter roll number: "))
        dict["name"]=input("enter the name: ")
        pickle.dump(dict,file)  #dump-to write in student file
    file.close()
 
def display():
    #read from file and display
    file=open("D:\\stud2.dat","rb")   #r-read,b-binary
    try:
        while True:
            stud=pickle.load(file)   #write to the file
            print(stud)
    except EOFError:
        pass
    file.close()
 
def search():
    file=open("D:\\stud2.dat","rb")   #r-read,b-binary
    r=int(input("enter the rollno to search: "))
    found=0
    try:
        while True:
            data=pickle.load(file)   #read from file
            if data["roll"]==r:
                print("The rollno =",r," record found")
                print(data)
                found=1
                break
    except EOFError:
        pass
    if found==0:
        print("The rollno =",r," record is not found")
    file.close() #main program while True: print("MENU \n 1-Write in a file \n 2-display ") print(" 3-search\n 4-exit \n") ch=int(input("Enter your choice = ")) if ch==1: write_in_file() if ch==2: display() if ch==3: search() if ch==4: print(" Thank you ") sys.exit()


Output 

MENU

 1-Write in a file
 2-display
 3-search
 4-exit

Enter your choice = 1
ENTER NO OF STUDENTS: 1
Enter details of student  1
Enter roll number: 01
enter the name: Ravi
MENU
 1-Write in a file
 2-display
 3-search
 4-exit

Click here to download Full Project File 

If you any doubts, Please let me know

Previous Post Next Post

Contact Form