Without getting really technical - it sounds like your input file was created somewhere else and got garbage in it - or somebody is doing something dumb when writing the data.
I would suspect the files first - try figuring out where in the file you first go off, then use od to examine the data a few records before the offset problem occurs -
od -x -j nnnnn filename | pg
Or - see if the file is the problem to start with -
example lrl = 120 bytes
let filelen=`cat file | wc -c`
result=`expr $filelen % $lrl`
if [ result -ne 0 ] ; then
echo "Houston, we have a problem"