Tcl channels: files, fconfigure, encodings and binary data

11 steps · about 3 h 25 min · Intermediate

  1. File Access 101 (tutorial, Tcl official tutorial, 20 min)

    Start with the basics of open, gets, puts, read and close, because every channel option builds on these.

  2. Input & output in Tcl (article, ZetCode, 15 min)

    It introduces the idea that stdin, stdout and files are all channels, with short worked scripts.

  3. Tcl open - File Operations in Tcl (article, ZetCode, 15 min)

    It covers access modes, permissions and the rb flag, so you see how binary mode first appears at open time.

  4. How do I read and write files in Tcl (reference, Tcler's Wiki, 15 min)

    It collects common idioms and pitfalls for line-by-line reading and whole-file reading.

  5. fconfigure manual page - Tcl Built-In Commands (doc, tcl.tk, 30 min)

    This is the official reference for -encoding, -translation, -blocking and the other channel options.

  6. Tcl Built-In Commands - fconfigure manual page (doc, tcl-lang.org, 15 min)

    Its PPM image example shows a channel switching from text mode to binary mode part-way through a read.

  7. encoding - the Tcler's Wiki! (reference, Tcler's Wiki, 10 min)

    Practical examples such as cp1252 conversion help you debug garbled text.

  8. Working with binary data (reference, Tcler's Wiki, 20 min)

    It explains why -translation binary is safer than -encoding binary alone, and shows a full write-then-read round trip.

  9. binary manual page - Tcl Built-In Commands (doc, tcl-lang.org, 35 min)

    Read this once your channels are in binary mode, to learn the format and scan field codes, endianness and unsigned flags.

  10. Tcl binary - Binary Data Manipulation in Tcl (article, ZetCode, 15 min)

    Short runnable examples of format, scan and base64 encoding make the manual page easier to follow.

  11. binary scan (reference, Tcler's Wiki, 15 min)

    It covers the signed-versus-unsigned trap and bit-field edge cases that you tend to hit in real file formats.