File IO - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

File IO

Description:

open-read-write-close is the basic I/O Model. gets read a line from channel ... Windows: A carriage return and a line feed sequence(rn, 0x0d 0x0A) Macintosh: ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 18
Provided by: coope
Category:
Tags: carriage | file

less

Transcript and Presenter's Notes

Title: File IO


1
File IO
2
Outline
  • Reading and writing files
  • Input/Output Commands
  • I/O Buffering
  • Blocking and Non-Blocking IO
  • End of Line (EOL) translation
  • fconfigure I/O channel options
  • fileevent command
  • Practices
  • References

3
Reading and writing files
  • Reading file
  • set fd open c/autoexec.bat r
  • while gets fd line gt 0
  • puts line
  • close fd

Open Read / Write Close
4
Reading and writing files
  • Writing file
  • set fd open c/output.txt w
  • gets stdin line
  • while line ! "quit"
  • puts fd line
  • gets stdin line
  • close fd

Open Read / Write Close
5
Reading and writing files
  • open-read-write-close is the basic I/O Model
  • gets read a line from channel
  • read read a block of data from channel.
  • Standard I/O channels are opened by default
    stdin, stdout, stderr
  • Network socket is just like File I/O, but instead
    of using open command, we use socket command to
    create a socket

6
Input/Output Commands
7
I/O Buffering
  • For I/O efficiency, Tcl buffers data in the
    memory by default. You can change buffering mode
    to one of the following using fconfigure command
  • fullI/O system will buffer output until its its
    internal buffer is full or until the flush
    command is invoked.
  • lineEach newline in an output channel or a flush
    command causes a write operation.
  • noneTurns off buffering, data will be wrote to
    the channel immediately.

8
I/O Buffering
9
Blocking and Non-Blocking IO
  • By default, I/O channels are blocking.
  • A gets or read will wait until data is available
    before returning.
  • A puts may also wait if the I/O channel is not
    ready to accept data.
  • This behavior is all right if you are using disk
    files, which are essentially always ready. If you
    use pipelines or network sockets, however, the
    blocking behavior can hang up your application.
  • None-Blocking I/O
  • Use fconfigure command to change to non-blocking
    mode
  • fconfigure channelId blocking 0
  • A gets or read command may return immediately
    with no data. This occurs when there is no data
    available on a socket or pipeline.
  • Nonblocking channels are useful because your
    application can do something else while waiting
    for the I/O channel
  • For non-blocking channels to work correctly,
    better use with the fileevent command.

10
End of Line (EOL) translation
  • End Of Line character differs from platforms
  • UNIX A single line feed character(\n, 0x0A)
  • Windows A carriage return and a line feed
    sequence(\r\n, 0x0d 0x0A)
  • Macintosh A single carriage return character(\r,
    0x0d)
  • puts, gets and read commands do EOL conversion to
    platform-native format. (That is on windows
    platform, Tcl translates EOL to \r\n when you
    write to file.)

11
End of Line (EOL) translation
Sample to illustrate EOL (End Of Line)
character set out open hello.txt w puts out
Hello puts out World close out
  • On Windows, the content of hello.txt will be

12
End of Line (EOL) translation
Sample to illustrate EOL (End Of Line)
character set out open hello.txt w fconfigure
out translation lf puts out Hello puts out
World close out
  • After changing EOL translation to lf (UNIX
    style), the content of hello.txt will be

13
End of Line (EOL) translation
  • End Of Line translation modes
  • When you should turn off translation?
  • Default EOL translation (auto) is good for text
    file manipulation.
  • When you handling binary files such as picture,
    video files, you should change to binary
    translation mode to turns off translation.

14
fconfigure I/O channel options
15
fileevent command
fileevent command registers a script that is
called when an I/O channel is readable or
writable. The advantage of this approach is that
your application can do other things, like update
the user interface, while waiting for data from
the pipeline or socket.
  • In this sample, script registered by fileevent
    will be called whenever sock becomes
    readable.
  • set sock socket 127.0.0.1 25
  • fconfigure sock buffering line blocking 0
  • fileevent sock readable
  • if !eof sock
  • puts gets sock
  • puts sock "HELO coopermaa"

You can refactor fileevent code as proc
GetData channel if !eof channel
puts gets channel fileevent sock
readable list GetData sock
16
Practices
  • Implement a type command
  • Implement a UNIX cat command

17
References
  • Practice Programming in Tcl and Tk 4th Edition
    Chap9, 16, Brent Welch, Prentice Hall, 2003/06
Write a Comment
User Comments (0)
About PowerShow.com