The problem is one of decoding data from a data source. This is usually handled with parsers. Neither static nor dynamic typing could really save you here, unless the file is written out in a format that could be automatically parsed by the input. (like S-expressions perhaps for a lisp read call...)
If it was as easy as your code would like it to be, people wouldn't have bothered with things like XDR, XML, ASN.1 (BER) etc. They'd just send binary data everywhere. How can you tell a 32bit value from four 8 bit bytes for instance?
You must parse I think.