Re: Help with binary files, read, and eof

From: Ulrich Schöbel (usus_at_tclers.tk)
Date: 02/03/04


Date: Tue, 03 Feb 2004 14:50:25 +0100

Hi Mark,

the changes below should help.

Best regards

Ulrich

Mark Curry wrote:
>
> I'm attemping to read binary files, and am running into issue with
> respect to EOF. Been through the FAQ and the wiki for info on using
> binary files, but can't seem to find the problem.
>
> Short description: I'm reading in a binary file, 255 bytes at a time.
> For each 255 byte "packet", I prepend 5 bytes, and send the whole thing
> out a filehandle configured for the serial port.
>
> My problem is with EOF handling. My files that I'm processing are
> always an integer multiple off 255 bytes in length. The current file
> I'm using is 9945 bytes = 255 bytes * 39 "packets".
>
> When running my script, however, it always outputs the first 39
> packets, without a problem, but then one additional packet
> of garbage data. I've stared at my EOF handling for hours, and
> cannot pick out the problem.
>
> Here's the main code, snipped to as short as I can manage.
>
> set count 0
> if [ catch { open $filename r } fh1 ] {
> puts stderr "Cannot open $filename : $fh1"
> } else {
> fconfigure $fh1 -translation binary -encoding binary
> set noquit 1
> while { $noquit } {
> set read_bytes [ read $fh1 255 ]
Insert your 'check for EOF' here

> set read_bytes_length [ expr [ string length $read_bytes ] ]
>
> # Send out Header to serial port ($fh)
> puts -nonewline $fh \xa0
> puts -nonewline $fh \x1e
> puts -nonewline $fh \x56
> puts -nonewline $fh \x05
> puts -nonewline $fh \x00
> flush $fh
>
> # Send out data to serial port
> if { $read_bytes_length != 0 } {
> puts -nonewline $fh $read_bytes
> puts stderr "Packet $count. read_bytes_length=$read_bytes_length"
> incr count
> }
> flush $fh
>
> # Check for EOF
> if [ eof $fh1 ] {
> for { set i $read_bytes_length } { $i < 255 } { incr i } {
> puts $fh \x00 # If, for some reason filesize is not an
> # integer number of packets, then pad zeros
> }
> set noquit 0
continue

> } elseif { $read_bytes_length != 255 } {
> puts stderr "ERROR read_bytes_length!=255, and EOF NOT detected."
> }
> }
> close $fh1
> }
>
> The script shows "Packet 39. read_bytes_length=255" being sent out. Only
> packets 0 - 38 should be sent. (I keep thinking it some basic problem
> with counting from 0 / counting from 1, but I can't see it).
>
> Any help appreciated.
> Using Tcl v8.4.4
>



Relevant Pages

  • [SUMMARY] Knowing EOT or EOF from a shell script
    ... Knowing EOT or EOF from a shell script ... I have a group of binary files with a big variable file size ... My duty is to know(through a Shell script) when each file fully ... and completely transferred to the shared location then transfer ...
    (SunManagers)
  • Re: EOF (novice)
    ... Simple -- escape codes. ... For example, the EOF ... closer to what I think James means by "a compressed filesystem." ... > your scheme simply does not work for binary files. ...
    (comp.lang.c)
  • Help with binary files, read, and eof
    ... binary files, but can't seem to find the problem. ... For each 255 byte "packet", I prepend 5 bytes, and send the whole thing ... My problem is with EOF handling. ... problem with counting from 0 / counting from 1, ...
    (comp.lang.tcl)
  • Re: Linux slack space question
    ... placing an EOF at the end, leaving the rest to slack space. ... files are binary files, i.e., there are no special characters inserted. ... The number of bytes actually written to files are handled in inodes. ...
    (Linux-Kernel)