Reading A Binary File

I have an assignment to develop a program that reads a binary file. The instructions say:

"The structure of the file will be such that you will read records of variable length. Each record is preceded by a two-byte field (RDW) which gives the length of the record that follows."

I am slightly confused by this, he did not give us a binary file to read and told us to create our own, We explained to him we did not now how to do this so what i did was create a text file with a list of names using notepad, opened it in WinHex and save it as a .bin file Is this the correct way to do this? also here is my code that reads the file. All it outputs is the number 97. I have included the text file that i opened in winhex and saved as a .bin

Re: Reading A Binary File

It makes sense that your program outputs a single number 97; you're reading only a single byte and your file is not empty (it starts with "anthony"); so number is not -1, it is 97 which is the unicode for a lower case 'a'.

kind regards,

Jos

I have the stamina of a seal; I lie on the beach instead of running on it.

It makes sense that your program outputs a single number 97; you're reading only a single byte and your file is not empty (it starts with "anthony"); so number is not -1, it is 97 which is the unicode for a lower case 'a'.

Re: Reading A Binary File

That's a start. The code reads the bytes and displays their int values.

Your assignment has some byte manipulation requirements.
If you have a hex editor, you can create a file in the required format for the input file. If the values between the | are bytes and ASCII char values are in 's, then the file's contents could look like this:
|0|4|'t'|'h'|'i'|'s'|0|2|'i'|'s'|0|1|'a'|0|4|'W'|' o'|'r'|'d'|

Re: Reading A Binary File

Thank you, i don't even really understand what the assignment requires he didn't really explain it well nor did he tell us how to make the actual binary file. My question is what exactly is the program supposed to output? The length of the record? so in your example of |0|4|'t'|'h'|'i'|'s'|0|2|'i'|'s'|0|1|'a'|0|4|'W'|' o'|'r'|'d' would the program output 04 02 01 04? Is it even supposed to output anything at all? All it says is develop a program that reads a binary file, then the instructions i put in the previous post. i made a bin file in winHex that follows your example it reads 04this02is01a04word

Originally Posted by Norm

That's a start. The code reads the bytes and displays their int values.

Your assignment has some byte manipulation requirements.
If you have a hex editor, you can create a file in the required format for the input file. If the values between the | are bytes and ASCII char values are in 's, then the file's contents could look like this:
|0|4|'t'|'h'|'i'|'s'|0|2|'i'|'s'|0|1|'a'|0|4|'W'|' o'|'r'|'d'|

Re: Reading A Binary File

Originally Posted by IceNine

I am slightly confused by this, he did not give us a binary file to read and told us to create our own, We explained to him we did not now how to do this so what i did was create a text file with a list of names using notepad.

You may have already figured this out but all computer files are binary files regardless of what the file suffix is. The suffix only provides information as to how the binary information is to be interpreted. So changing the suffix doesn't (or at least shouldn't) change the contents.

Caveat: It is common practice to refer to files as either text or binary. This serves to distinguish between files that either do or do not contain only character data.

Re: Reading A Binary File

Your assignment was to write a program that reads the described file and extracts the records from the file.

Text files use endline characters (\r\n) to tell the reading program where the end of the record/line is.

The "binary" file you are to read uses two bytes to hold the length of the record followed by the bytes for the record.
In my example,
the first record was 4 bytes long and had the characters: this
the next record was 2 bytes long and had the characters: is

Your assignment was to write a program that reads the described file and extracts the records from the file.

Text files use endline characters (\r\n) to tell the reading program where the end of the record/line is.

The "binary" file you are to read uses two bytes to hold the length of the record followed by the bytes for the record.
In my example,
the first record was 4 bytes long and had the characters: this
the next record was 2 bytes long and had the characters: is

Re: Reading A Binary File

The file shown in post#11 is close. But, it only has a single byte for the length of the record. I thought the specs said that there were two byes for the record length (allow 32K length(or 64K if unsigned)). A one byte length field would allow 127/255 byte record length.

Re: Reading A Binary File

Could this be done by simply changing the length of their names so the preceding value is two bytes in length. For example 23 Wolfgang Amadeus Mozart 20 Ludwig van Beethoven etc....

Originally Posted by Norm

The file shown in post#11 is close. But, it only has a single byte for the length of the record. I thought the specs said that there were two byes for the record length (allow 32K length(or 64K if unsigned)). A one byte length field would allow 127/255 byte record length.

Re: Reading A Binary File

The first two bytes contain the length of the following record in binary.
For example the first two bytes in hex for a record of length 20 would be: 00|14|
For example the first two bytes in hex for a record of length 256 would be: 01|00|

Re: Reading A Binary File

How does the readLine() method stop reading at the end of the record? I don't see where the code uses the 2 byte record length value.
BTW the formatting needs some work. There should not be }s one above the other like in lines 34-37
Nested statements should be indented 3-4 spaces.