Reading Binary Files (ITI8510)
Allikas: Lambda
Real-Time Operating Systems and Systems Programming |
[ @ ] |
This practice is about writing data in binary format.
Task
There are two tasks; the first one is simple fread() fwrite() to get the general idea accross, the second needs some thinking.
Runtimes
- Write a program which counts and prints how many times it has been run.
- Save the amount of runs into a file named runtimes.dat
- If the file is missing, write a binary 1 into the file, save and exit.
- If the file exists, read the count from the file, increment it, print it and write the new count into the file before exiting.
Array to file and from file
Here we could also learn using getopt() function.
- If the program starts without command line options, show help
- If program is run with
table -c filename.dat -n 10
- Create 10x10 element 2d array (okay to create just a multiplication table here) and write it into file named filename.dat (save size, then elements)
- If program is run with
table -r filename.dat
- Open the file, read the array dimensions if stored, show the array on the screen.
äidake tabel ekraanile.
Hints
- It is advisable to store the table so that the first 4 bytes of the file hold the table dimensions, then the array elements would follow.
- If the numbers in file are human-readable you are doing the binary wrong.