Prev Up Next Index
Go backward to 9.2 chkform
Go up to 9 Format Conversion
Go forward to 9.4 Creating a Binary Archive

9.3 readfile

 

FreeForm ND includes readfile, a simple interactive binary file reader. The program has one required command line argument, the name of the file to be read. You do not have to write format descriptions to use readfile.

The readfile command has the following form:

readfile binary_data_file

When the program starts, it shows the available options, shown in table 9.3. At the readfile prompt, type these option codes to view binary encoded values. (Pressing return repeats the last option.)

The readfile program options
 

c char -- 1 byte character
s short -- 2 byte signed integer
l long -- 4 byte signed integer
f float -- 4 byte single-precision floating point
d double -- 8 byte double-precision floating point
uc uchar -- 1 byte unsigned integer
us ushort -- 2 byte unsigned integer
ul ulong -- 4 byte unsigned integer
b Toggle between "big-endian" and your machine's native byte order
p Set new file position
P Show present file position and length
h Display this help screen
q Quit

The options let you interactively read your way through the specified binary file. The first position in the file is 0. You must type the character(s) indicating variable type (e.g., us for unsigned short) to view each value, so you need to know the data types of variables in the file and the order in which they occur. If successive variables are of the same type, you can press Return to view each value after the first of that type.

You can toggle the byte-order switch on and off by typing b. The byte-order option is used to read a binary data file that requires byte swapping. This is the case when you need cross-platform access to a file that is not byte-swapped, for example, if you are on a Unix machine reading data from a CD-ROM formatted for a PC. When the switch is on, type s or l to swap short or long integers respectively, or type f or d to swap floats or doubles. The readfile program does not byte swap the file itself (the file is unchanged) but byte swaps the data values internally for display purposes only.

To go to another position in the file, type p. You are prompted to enter the new file position in bytes. If, for example, each value in the file is 4 bytes long and you type 16, you will be positioned at the first byte of the fifth value. If you split fields (by not repositioning at the beginning of a field), the results will probably be garbage. Type P to find out your current position in the file and total file length in bytes. Type q to exit from readfile.

You can also use an input command file rather than entering commands directly. In that case, the readfile command has the following form:

readfile binary_data_file < input_command_file 

Tom Sgouros and James Gallagher, 2006-02-12

Prev Up Next