Binary File Handling Board Questions


  1. Consider a binary file, items.dat, containing records stored in the given format : [3 Marks] [2024]
    {item_id : [item_name, amount]}

    Write a function, Copy_new(), that copies all records whose amount is greater than 1000 from items.dat to new_items.dat .
    COMING SOON

  2. A binary file, EMP.DAT has the following structure : [3 Marks] [2024]
    [Emp_Id, Name, Salary]

    Where
    Emp_Id : Employee id
    Name : Employee Name
    Salary : Employee Salary

    Write a user defined function, disp_Detail(), that would read the contents of the file EMP.DAT and display the details of those employees whose salary is below 25000.
    coming soon

  3. Shreya is a programmer, who has recently been given a task to write a user defined function named write_bin() to create a binary file called cust_file.dat containing customer information - customer number (c_no), name (c_name), quantity(qty), price (price) and amount (amt) of each customer. The function accepts customer number, name, quantity and price. Thereafter, it displays the message β€˜Quantity less than 10…. Cannot SAVE’, if the quantity entered is less than 10. Otherwise the function calculates amount as price * quantity and then writes the record in the form of a list into the binary file. [5 Marks][2023]
    
    import pickle
    def write_bin( ):
    	bin_file = ______________	#Statement 1
    	while True:
    		c_no = int(input(β€œenter customer number”))
    		c_name = input(β€œenter customer name β€œ)
    		qty = int(input(β€œenter price”))
    		price = int(input(β€œenter price”))
    		if ________________		#Statement 2
    			print(β€œQuantity less than 10…Cannot SAVE”)
    		else:
    			amt = price * qty
    			c_detail = [c_no, c_name, qty, price, amt]
    			______________	#Statement 3
    			ans = input(β€œDo you wish to enter more records y/n”)
    			if ans.lower( ) = = β€˜n’:
    				____________	#Statement 4
    		________________________	#Statement 5
    	______________________________	#Statement 6
    
    a) Write the correct statement to open a file β€œCust_file.dat” for writing the data of the customer. [1]
    b) Which statement should Shreyas fill in Statement 2 to check whether quantity is less than 10. [1]
    c) Which statement should Shreyas fill in Statement 3 to write data to the binary file and in Statement 4 to stop further processing if the user does not wish to enter more records. [2]
    OR(Option for part (c) only)
    c)  What should Shreyas fill in Statement 5 to close the binary file named Cust_file.dat and in Statement 6 to call a function to write data in a binary file? [2]
    
    (i) Statement 1 : open("Cust_file.dat", "wb")
    (ii) Statement 2 : qty<10
    (iii) Statement 3 : pickle.dump(c_detail, bin_file)
    Statement 4 : break
    or
    Statement 5 : bin_file.close()
    Statement 6 : write_bin()