struct_read

Section: TclStruct Built-In Commands (n)
Updated:
Index Return to Main Contents
 

NAME

struct_read - Read from a stream to an object or field  

SYNOPSIS

struct_read ?flags? fileId obj|obj(elem) ?length?

 

DESCRIPTION

Read from a file specified by fileId (a Tcl file id) into an object or field. fileId must be stdin or the return value from a previous call to open; it must refer to a file that was opened for reading. The file is read as a binary stream of bytes directly into the object or field.

By default, the length to be read is the size of the object. If a length is specified, it may either be the integer length, or a variable that contains the integer length. When a variable name is specified, then the number of bytes read is stored in the variable as well as being returned to the caller.

The following optional flags may be specified in any order:

-eofok
Normally the struct_read command will generate an error the read fails because of end-of-file. Specifying -eofok will allow struct_read to return a value of 0 when EOF is read.
-unbuffered
Issue a raw read(2) instead of using fread(3S).
-partial
Normally struct_read will continue reading until it reads the full number of bytes wanted. Specifying -partial will cause struct_read to return the actual number of bytes read from the first read. Note that this will normally be used in conjunction with -unbuffered since otherwise the fread(3S) call will most likely do its own retries.
-mod4
Make sure the length read is a multiple of 4 by increasing the length read if necessary. It is the caller's responsibility to make sure the object is big enough to avoid overwriting the end of the object buffer.

struct_read returns the number of bytes read.

 

EXAMPLES

This example reads in the contents of an object from a file. set file [open 'filename' r] read $file obj close $file

 

WARNINGS

The data read is not machine independent.

Intermixing buffered and unbuffered I/O on the same fileID is a bad idea.

 

KEYWORDS

file, read, struct


 

Index

NAME
SYNOPSIS
DESCRIPTION
EXAMPLES
WARNINGS
KEYWORDS

This document was created by man2html, using the manual pages.
Time: 22:11:02 GMT, June 11, 2022