0% found this document useful (0 votes)
7 views

Binary Files

Uploaded by

audacityvtd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Binary Files

Uploaded by

audacityvtd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Course: Programming Techniques

BINARY FILE EXERCISES


For the following questions, you may write any additional functions if needed.

1. Write a program to read data from a binary file and display it on the console.

2. Write a program to write data to a binary file.

3. A binary file “Book.dat” has structure: [BookNo, BookName, Author, Price]


a. Write a C++ function InputBook to input data for a record and save to the file
Book.dat. If the file exists, add the new data to the end of the file.
b. Write a C++ function CountRec which receives the Author name as a parameter and
return the number of books by the given author in the file “Book.dat”.

4. A binary file “STUDENT.dat” has structure: [StudentID, Name, GPA]


a. Write a C++ function AddStudent to input data for a record and save to the file
STUDENT.dat. If the file exists, add the new data to the end of the file.
b. Write a C++ function PrintStudent which receives the GPA as a parameter and print
out all students (student ID and student name) in the file “STUDENT.dat” that have
the GPA ≥ the input parameter.

5. Write a C++ program to copy a binary file to a new binary file.

6. Write a C++ program to split a binary file into parts of 5 KB each. Use the name-00
format to call each part of the file.

7. Write a program in C++ that inverts all the bytes of a binary file. The new file has the
same name with the original file but with the extension .inv.

8. Create a hexadecimal viewer in C++ that shows the contents of a binary file on screen
with 16 bytes per row. The 16 bytes will be displayed first in hexadecimal and then as
printable characters. You must also substitute bytes smaller than 32 (unprintable
characters) with dots. Sample output is given below:

1. 00000000 4d 5a 90 00 03 00 00 00 04 00 00 00 ff ff 00 00 MZ?.........ÿÿ..
2. 00000010 b8 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 ¸.......@.......
3. 00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
4. 00000030 00 00 00 00 00 00 00 00 00 00 00 00 80 00 00 00 ............?...
5. 00000040 0e 1f ba 0e 00 b4 09 cd 21 b8 01 4c cd 21 54 68 ..º..´.Í!¸.LÍ!Th
6. 00000050 69 73 20 70 72 6f 67 72 61 6d 20 63 61 6e 6e 6f is program canno

Lecturer: Nguyễn Hải Minh – University of Science, HCM city –


1 [email protected]
7. 00000060 74 20 62 65 20 72 75 6e 20 69 6e 20 44 4f 53 20 t be run in DOS
8. 00000070 6d 6f 64 65 2e 0d 0d 0a 24 00 00 00 00 00 00 00 mode....$.......
9. 00000080 50 45 00 00 4c 01 03 00 b2 7c cd cf 00 00 00 00 PE..L...²|ÍÏ....
10. 00000090 00 00 00 00 e0 00 22 00 0b 01 30 00 00 0a 00 00 ....à."...0.....

9. Write a C++ function to read ID3 v1 tag from an MP3 music file and print it to console.
Below is the structure of ID3 version 1:
It consists of attaching a block of fixed size of 128 bytes at the end of the file in
question. This block contains the following tags:
a. An identification header with the characters "TAG".
b. Title: 30 characters.
c. Artist: 30 characters.
d. Album: 30 characters.
e. Year: 4 characters.
f. One comment: 30 characters.
g. Genre (musical): a character.
All tags use ASCII characters, except the genre, which is an integer stored in a single
byte. The musical genre associated with each byte is predefined in the standard and
includes definitions of 80 genres, numbered from 0 to 79. Certain reproduction
programs have expanded on their own the defined genres (from number 80).

10. Write a program in C++ to read the dimensions of an image in Windows bitmap format.
Description of the BMP file can be viewed here:
https://www.fileformat.info/format/bmp/corion.htm
Firstly, you should first check that it is a valid .bmp image, reviewing the header data
'BM'. If it is a valid .bmp image then it obtained its dimensions (width x height) and
display them on the screen.
Sample BMP file can be downloaded here:
https://www.fileformat.info/format/bmp/sample/index.htm

Lecturer: Nguyễn Hải Minh – University of Science, HCM city –


2 [email protected]

You might also like