WRITE
WRITE [ # Flujo , ] Expresión [ , Longitud ]
WRITE [ # Puntero , ] Expresión [ , Longitud ]
Escribe una expresión en un
Flujo usando su representación binaria.
Si no se especifica el flujo, se emplea la salida estándar.
Si la
Expresión es una cadena, puede especificar la longitud que indica el número de bytes a escribir. Si no se especifica la longitud, éste se escribe en el flujo justo antes de la cadena de datos.
 |
Esta función emplea el orden de bytes del flujo para leer datos.
|
Si especifica un
puntero en lugar de un
flujo, los datos serán escritos directamente en la dirección de memoria especificada por el puntero.
Tenga cuidado con el tipo de dato del argumento Expresión
 |
El tipo de datos de Expresión se utiliza para saber qué escribir exactamente al Flujo. Tenga en cuenta que no es necesariamente lo que piensas.
Por ejemplo, si tiene una referencia anónima sobre un objeto (es decir, si utiliza el tipo de datos Object, entonces el tipo de datos de cualquiera de sus propiedades será Variant, no su tipo de datos reales. Y así lo habra escrito, escribirá un Variant, no lo que esperaba.
Si utiliza una referencia de objeto, cuyo tipo de datos se declara en tiempo de compilación, no tendrá esta problem.
Si no está seguro, la forma más segura es usar una variable intermediaria local con un tipo de datos conocidos, o una conversión explícita.
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 write a float property
' The bad way
WRITE #hStream, hObject.Value
' The good way
fValue = hObject.Value
WRITE #hStream, fValue
' Another good way
WRITE #hStream, CFloat(hObject.Value)
|
Ejemplo
... ' The following bytes will be written to the stream (hexadecimal notation) :
WRITE #hfile, "123456789" ' 09 31 32 33 34 35 36 37 38 39
WRITE #hfile, "123456789", 0 ' 09 31 32 33 34 35 36 37 38 39
WRITE #hfile, "123456789", 4 ' 31 32 33 34
WRITE #hfile, "123456789", 11 ' 31 32 33 34 35 36 37 38 39 00 00
WRITE #hfile, "123456789", -2 ' Nothing will be written to the stream.
...
Este ejemplo muestra como se puede escribir un archivo binario. Despues de leer el archivo creado se puede mostrar el contenido.
PUBLIC SUB ButtonWriteBinary_Click()
DIM filePath AS String
' Use a temporary file
filePath = Temp()
' Write binary file
BinaryWrite(filePath)
' Display binary file
BinaryRead(filePath)
' Remove temporary file
KILL filePath
CATCH
Message.Error(Error.Text)
END
PRIVATE SUB BinaryWrite(FilePath AS String)
DIM binaryFile AS File
DIM i AS Integer = 10
DIM b AS Byte = 4
DIM s AS Short = 23
DIM s1 AS String = "This is string 1"
DIM s2 AS String = "Another string"
' Open as create so we get a new file
binaryFile = OPEN FilePath FOR CREATE
WRITE #binaryFile, i
WRITE #binaryFile, b
WRITE #binaryFile, s
WRITE #binaryFile, s1
WRITE #binaryFile, s2
CLOSE #binaryFile
END
PRIVATE SUB BinaryRead(FilePath AS String)
DIM binaryFile AS File
DIM i AS Integer
DIM b AS Byte
DIM s AS Short
DIM s1 AS String
DIM s2 AS String
' Read binary file
binaryFile = OPEN FilePath FOR READ
READ #binaryFile, i
READ #binaryFile, b
READ #binaryFile, s
READ #binaryFile, s1
READ #binaryFile, s2
CLOSE #binaryFile
' Display results
PRINT i
PRINT b
PRINT s
PRINT s1
PRINT s2
END