Re: tcl "binary format" error

From: Andreas Leitgeb (avl_at_gamma.logic.tuwien.ac.at)
Date: 03/24/04


Date: 24 Mar 2004 12:27:59 GMT

joachim schuessler <schuessler@hhi.fhg.de> wrote:
> set env_fl [ open $nm w ]
> for { set i 0 } { $i <= 255 } { incr i } {
> puts -nonewline $env_fl [ binary format c $i ]
> }
> close $env_fl
> The hex_test.bin File contains
> .. a2 a3 3f a5 3f a7 3f a9 ...... b3 3f b5 b6 b7 3f b9 ba bb 3f 3f 3f bf
> correct is
> .. a2 a3 a4 a5 a6 a7 a8 a9 ...... b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf

Those "3f"'s you're seeing are question-marks, that are substed
for characters that tcl thinks it cannot convert to your native
charset during writing to file. This "native charset" (as seen by
tcl) is what varies between the platforms (or distributions).

using:
fconfigure $env_fl -translation binary
immediately after the line doing the [open ...]
will avoid this problem, because it will tell
Tcl not to convert at all.

This raises the question, whether:
  [open $fname wb] (or rb) could be made to internally
     do the fconfigure to binary, since this seems to be the
     intent of the "b" flag in ansi-C's fopen(), iirc.
     (for the list-form of open's access-parameter, "BINARY"
     would seem to be a fit keyword)
Just for the elegance of it.

(currently playing with parsing and composing of midi-files, I
 was quite often reminded that I had to spell out the fconfigure
 to get reading and writing work correctly for binary formats)



Relevant Pages

  • Re: tclhttpd with utf-8
    ... fconfigure $sock -translation binary -blocking ... I'm still trying to figure out why I get corrupted filenames from [glob ... It's shortsighted of Tcl developers to think people wouldn't want this, especially with GUIs, and massive million file directories. ... For me the TCT members are just some tcl users who know its internals a little better, and devote some of their free time to help make tcl better. ...
    (comp.lang.tcl)
  • Re: md5::md5 returns unexpected value,am I using it correctly?
    ... Windows only. ... And in Tcl on Windows you have to remember to fconfigure ... Basically two translations are happening: ...
    (comp.lang.tcl)
  • subprocess output handling
    ... being sent, a Tcl process receiving the notice, looking up the job ... ran into a problem with interrupted I/O and throwing a Tcl error when ... fconfigure $fh -buffering none ... This is on a system running Fedora Core 1, ...
    (comp.lang.tcl)
  • line buffering while reading from command pipe
    ... I have a program B I want to run from Tcl. ... my reader IS line buffered (even if I have the reader ... fconfigure -buffering full). ... puts "error opening pipe" ...
    (comp.lang.tcl)