READ[../../def/stream] _\Stream_
READ [ # Stream , ] Variable [ , Length ]
READ [ # Pointer , ] Variable [ , Length ]
Lee los
Flujo Stream como binarios cuyo tipo el es dado por el tipo de
variable. Las representaciones binarias es utilizada por la instrucción
WRITE.
Si el
Flujo no esta especificado, entonces se usa la entrada estándar.
Si la
Variable es string(cadena), usted puede especificar una longitud que indique el número de bytes a leer, si la longitud es negativa, entonces(-
Length) bytes son leídos hasta el final de
Flujo.
Si la longitud no es especificada por una cadena, o si la longitud es cero, entonces la longitud de la string es leída del
Flujo. La cadena debe haber sido escrita con la instrucción
WRITE
 |
Esta instrucción usa las ordenes de bytes del Flujo para leer los datos
|
Si especificas un
Pointer en vez de un
Stream, entonces el dato puede ser leído directamente de la dirección de memoria especificada por el puntero.
 |
Si tratas de leer de una dirección de memoria prohibida, obtendrás un error. The interpreter won't crash.
|
Tenga cuidado con los tipos de datos al agregar a una Variable
 |
El tipo de datos de una Variable es usado para saber que leer exactamente de un Flujo. Tenga en cuenta que no es necesariamente lo que piensas.
The datatype of is used to know what to read exactly from the Flujo. Beware that it is not necessarily what you think.
Por ejemplo si usted tiene una referencia anónima de un objeto(ej. Si usa el tipo de dato Object), entonces el tipo de dato de cualquiera de estas propiedades podría llegar a ser Variant, no es un tipo de dato real. Y por lo tanto READ puede leer una variante no esperada.
If you use an object reference whose datatype is declared at compilation time, you won't have this problem.
If you are not sure, the safest way is to use an intermediate local variable with a well-known datatype.
DIM hStream AS Stream ' The stream
DIM hObject AS Object ' An object with a float property named "Value"
DIM fValue AS Float ' We want to read a float property
' The bad way
READ #hStream, hObject.Value
' The good way
READ #hStream, fValue
hObject.Value = fValue
|
Example
This example demonstrates how to read a binary file. It will prompt the user to open a wav music file in order to display information from the wav header. It
OPEN's the file and then uses
SEEK to locate the start of the wav file header. Then we use READ to get the wav header information. Note that we need to read the right number of bytes for each
variable. Hence some of the data types we read are
Short and some are
Integer. Finally we simple display the information.
PUBLIC Type AS Short
PUBLIC Channels AS Short
PUBLIC SampleRate AS Integer ' Hz
PUBLIC BytesPerSecond AS Integer ' Bytes
PUBLIC Alignment AS Short
PUBLIC SampleSize AS Short ' Bits
PUBLIC FileSize AS Integer ' Bytes
PUBLIC Duration AS Float ' Seconds
PUBLIC SUB ButtonRead_Click()
DIM wav AS File
Dialog.Filter = ["*.wav", "WAVE music files"]
IF Dialog.OpenFile() THEN RETURN
' We need the file size to calculate the duration
FileSize = Stat(Dialog.Path).Size
wav = OPEN Dialog.Path FOR READ
SEEK #wav, 20 ' The info we want starts 20 bytes into the file
READ #wav, Type
READ #wav, Channels
READ #wav, SampleRate
READ #wav, BytesPerSecond
READ #wav, Alignment
READ #wav, SampleSize
CLOSE #wav
' Calc duration in seconds
Duration = FileSize / BytesPerSecond
' Display results
PRINT "Type " & Type
PRINT "Channels " & Channels
PRINT "Sample Rate (Hz) " & SampleRate
PRINT "Bytes Per Second " & BytesPerSecond
PRINT "Alignment " & Alignment
PRINT "Sample Size (Bytes) " & SampleSize
PRINT "File Size (Bytes) " & FileSize
PRINT "Duration (Seconds) " & Duration
CATCH
PRINT Error.Text
END