0% found this document useful (0 votes)
116 views3 pages

Binary File Operations and Structures

Uploaded by

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

Binary File Operations and Structures

Uploaded by

notneedtome
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1 Mark Questions

1. The process of converting byte stream back to the original structure is known as
a. Picklingb. b. Unpickling c. Packing d. Zipping
2. Which file mode is used to handle binary file for reading.
a. rb b. rw c. r d. w
3. Which of the following is not a correct statement for binary files?
a. Easy for carrying data into buffer b. Much faster than other file systems
c. Characters translation is not required d. Every line ends with new line character ‘\n’
4. Which one of the following is correct statement?
a. import – pickle b. pickle import c. import pickle d. All the above
5. Which of the following file mode opens a file for append or read a binary file and moves the
files
pointer at the end of the file if the file already exist otherwise create a new file?
a. a b. ab c. ab+ d. a+
6. Which of the following file mode opens a file for reading and writing both as well as
overwrite the
existing file if the file exists otherwise creates a new file?
a. w b. wb+ c. wb d. rwb
7. Mr Sharma is working on a binary file and wants to write data from a list to a binary file.
Consider list
object as l1, binary file sharma_list.dat, and file object as f. Which of the following can be the
correct
statement for him?
a. f = open(‘sum_list’,’wb’); [Link](l1,f)
b. f = open(‘sum_list’,’rb’); l1=[Link](f)
c. f = open(‘sum_list’,’wb’); [Link](l1,f)
d. f = open(‘sum_list’,’rb’); l1=[Link](f)
8. Every file has its own identity associated with it. This is known as:
a. icon b. extension c. format d. file type
9. EOL in a file stands for :
a. End of Lines b. End of Line c. End of List d. End of Location
10. Which of the following file types allows you to store large data files in the computer
memory?
a. Binary Files b. Text Files c. CSV Files d. None of these
2 Marks Questions
1. Write a program in python to write and read structure, dictionary to the binary file.
2. BINARY file is unreadable and open and close through a function only so what are the
advantages of
using binary file
3. Write a statement to open a binary file name [Link] in read mode and the file [Link]
is placed in
a folder ( name school) existing in c drive.
4. When do you think text files should be preferred over binary files?
5. Consider a binary file [Link] containing details such as empno:ename:salary (separator
':') write a
python function to display details of those employees who are earning between 20000 and
30000(both
values inclusive)
6. Differentiate between [Link]() and [Link]() methods with suitable examples.
7. A binary file “[Link]” has structure [BookNo, Book_Name, Author, Price].Write a user
defined
function CreateFile() to input data for a record and add to [Link]
8. A binary file “[Link]” has structure (admission_number, Name, Percentage). Write
a function
countrec() in Python that would read contents of the file “[Link]” and display the
details of those
students whose percentage is above 75.
9. A binary file “[Link]” has structure [ItemNo, Item_Name, Company, Price]. Write a
function
CountRec(Company) in Python which accepts the Company name as parameter and count and
return
number of Items by the given Company are stored in the binary file “[Link]”.
10. A binary file “[Link]” has structure [ItemNo, Item_Name, Company, Price]. Write a
function
AddRecord() which accepts a List of the record [ItemNo, Item_Name, Company, Price] and
appends in the
binary file “[Link]”.
3 Marks Questions
1. A binary file “[Link]” has structure [BookNo, Book_Name, Author, Price].
i. Write a user defined function CreateFile() to input data for a record and add to “[Link]” .
ii. Write a function CountRec(Author) in Python which accepts the Author name as parameter
and count
and return number of books by the given Author are stored in the binary file “[Link]”
2. A binary file “[Link]” has structure [Roll_Num, Name, Percentage]
i) Write a function Count_Rec() in Python that would read contents of the file “[Link]”
and
display the details of those students whose percentage is below 33.
ii) Write a function Disp_Rec(alphabet) in Python that would read contents of the file
“[Link]”
and display the details of those students whose name begins with the alphabet as passed as
parameter to the
function.
3. A binary file “[Link]” has structure [ITEMID, ITEMNAME, QUANTITY, PRICE].
Write a user
defined function MakeFile( )to input data for a record and add to [Link].
4. Write a function GetPrice(ITEMID) in Python which accepts the ITEMID as parameter and
returns
PRICE of the Item stored in Binary file [Link].
5. A binary file “[Link]” has structure (EMPID, EMPNAME, SALARY). Write a
function
CountRec( ) in Python that would read contents of the file “[Link]” and display the
details of
those Employees whose Salary is above 20000.
6. A binary file “[Link]” has structure (EMPID, EMPNAME, SALARY). Write a
function to
display number of employees having Salary more than 20000.
7. A binary file named “[Link]” has some records of the structure [EmpNo, EName, Post,
Salary], Write
a user-defined function named NewEmp() to input the details of a new employee from the user
and store it
in [Link].
8. Write a user-defined function named SumSalary(Post) that will accept an argument the post of
employees & read the contents of [Link] and calculate the SUM of salary of all employees of
that Post.
9. A binary file named “[Link]” has some records of the structure [TestId, Subject,
MaxMarks,
ScoredMarks] Write a function in Python named DisplayAvgMarks(Sub) that will accept a
subject as an
argument and read the contents of [Link].
10. Write a python program to search and display the record of the student from a binary file
“[Link]”
containing students records (Rollno, Name and Marks). Roll number of the student to be
searched will be
entered by the user.
5 Marks Questions
1. A binary file “[Link]” has structure [rollno, name, marks].
i. Write a user defined function insertRec() to input data for a student and add to [Link].
ii. Write a function searchRollNo(r) in Python which accepts the student’s rollno as parameter
and searches
the record in the file “[Link]” and shows the details of student i.e. rollno, name and marks (if
found)
otherwise shows the message as ‘No record found’.
2. Write a python program to create binary file [Link] and write 10 records in it:
Dvd id,dvd name,qty,price
Display those dvd details whose dvd price is more than 25.

Common questions

Powered by AI

Text files are preferable over binary files when the data primarily consists of human-readable characters, such as plain text data, and there is a need for easy manual editing or readability by humans . Additionally, text files are preferred when portability across different systems is important since they do not depend on platform-specific encodings or byte order . Text files are also simpler to debug due to their readability. However, these advantages come with trade-offs in terms of file size and processing speed when compared to binary files.

Define a function that opens 'employee.dat' in read-binary mode (`open('employee.dat', 'rb')`). Use `pickle.load(file)` in a loop to read each employee record. For each record, check if the employee's salary is between 20,000 and 30,000 inclusive. If so, display these employee details. End the loop upon catching an EOFError to signify the end of the file .

To add a new record to "Book.dat", define a function `CreateFile()`. Prompt the user for inputs: BookNo, Book_Name, Author, and Price. Open "Book.dat" in append-binary mode using `open('Book.dat', 'ab')`. Create a dictionary (or tuple) for the record. Use `pickle.dump(record, fileObj)` to append the record to the file, thereby preserving existing data and seamlessly integrating the new entry .

The function pickle.dump() is used to serialize and write an object to a binary file by converting it into a byte stream, whereas pickle.load() is used to read and deserialize the byte stream back into a Python object. For example, to write a dictionary to a file, you would use `pickle.dump(dictionary, fileObj)` where `fileObj` is opened in write binary mode. To read it back, you would use `pickle.load(fileObj)`, where `fileObj` opens the file in read binary mode .

Binary files have several advantages over text files, including faster data processing and no need for character transformation, which makes handling binary data more efficient . Binary files are also better for managing non-text data, such as images or executable files, as they preserve the exact byte representation of the data without any encoding/decoding overhead. However, they are not human-readable, which means debugging them directly is more challenging compared to text files .

To search for a student record by roll number in a binary file "student.dat", open the file in read-binary mode (`open('student.dat', 'rb')`) and iterate through each record using a loop with `pickle.load(file)`. For each record, check if the roll number matches the desired value. If found, display the corresponding student details; otherwise, display a message indicating the record was not found. Be sure to handle EOFError to terminate the loop when reaching the end of the file .

To write a list of dictionaries to a binary file in Python, open the file in write-binary mode using `open('filename', 'wb')`, and use `pickle.dump(list_of_dicts, fileObj)`. To read this data back, open the file in read-binary mode using `open('filename', 'rb')`, followed by using `pickle.load(fileObj)` to deserialize the data back into a list of dictionaries . These operations efficiently serialize and deserialize the data, preserving its structure and content.

To count the number of items from a specific company in "Store.dat", define function `CountRec(companyName)`. Open "Store.dat" in read-binary mode (`open('Store.dat', 'rb')`). Use `pickle.load(file)` inside a loop to read each item. Within the loop, check if the item's Company matches `companyName`, incrementing a counter if true. Handle EOFError to safely exit the loop when done, then return the counter .

The file mode "ab+" allows appending data to a binary file while also permitting reading. This mode positions the file pointer at the end of the file if it already exists, and if the file does not exist, it creates a new one .

The file mode "wb+" is used to open a binary file for both reading and writing. It overwrites the existing file if it exists or creates a new file if it does not . This mode is suitable for applications where both operations are needed, albeit with the caution that existing data will be lost if the file already exists.

You might also like