The open statement can be used to input/output files (I/O ). Syntax: Open pathname for mode [Access access] [lock] as [#] filenumber [Len = reclength] the syntax of an open statement includes the following parts: It is necessary to partially describe pathname. String expression that specifies the file name. The file name may also include directories, folders, and drives. Mode is required. Keyword: Specifies the file method, including append, binary, input, output, or random. If no method is specified,
Then open the file in random access mode. Access is optional. Keyword, indicating that the opened file can be operated, including read, write, or read write. Optional. Keyword, which indicates operations on files opened by other processes, including shared, lock read, lock write, and lock read.
Write operation. Filenumber is required. A valid file number ranging from 1 to 511. Use the freefile function to obtain the next available file number. Reclength is optional. It is less than or equal to 32,767 (bytes. For files opened in random access mode, this value is the record length. For
Sequential file. The value is the number of buffered characters. This indicates that you must open the file before performing any I/O operations on the file. The open statement allocates a buffer for the file to use for I/O, and determines
Access Method. If the file specified by pathname does not exist, you can create
This file. If the file has been opened by another process and the specified access type is not allowed, the open operation fails and an error occurs. If the mode is binary, the Len clause is ignored. It is important that you can use different file numbers in the binary, input, and random modes to open the same file, rather than closing the file first. In append and
In the output mode, if you want to open the same file with different file numbers, you must close the file before opening it. The get statement reads an opened disk file into a variable. The syntax of the get [#] filenumber, [recnumber] And varname get statements is as follows: it is necessary to partially describe filenumber. Any valid file number. Recnumber is optional. Variant (long ). Record Number (file in random mode) or number of bytes (file in binary mode), to indicate opening
Read data. Varname is required. A valid variable name that puts the read data in it. It indicates that put writes the data read by get to a file. The first record or byte in the file is at location 1, the second record or byte is at location 2, and so on. If recnumber is omitted, the system reads
The next record or byte after the get or put statement (or read the record or byte specified by the last seek function ). All commas used for demarcation are required
For example, get #4, filebuffer: The following rules apply to files opened in random mode: if the length of the data to be read is smaller than the length specified in the Len clause of the open statement, get reads the subsequent records within a certain boundary. Here,
The boundary length is equal to the record length. Fill in the existing content in the file buffer to a space. The space is between the end of a record and the next record.
. Because the entered data volume cannot be determined, we should try to make the record length consistent with the read data length. This is usually a good method. If the read variable is a variable-length string, the get statement first reads a dual-byte descriptor containing the string length, and then reads the number of values in the variable.
Data. Therefore, the length of the record specified by the Len clause in the open statement must be at least two bytes longer than the actual length of the string. If the read variable is a numeric type variant, get reads two bytes first, recognizes the vartype of the variant, and then reads the variable into it.
. For example, when reading the variant of vartype 3, get reads six Bytes: The first two bytes indicate that variant is vartype 3.
(Long). The last four bytes contain long data. The length of the record specified by the Len clause in the open statement must be at least the actual length required to store the variable.
The length is more than two bytes. Note that you can use the get statement to read a variant array from the disk, but you cannot use it to read the scalar variant that contains the array. You cannot use get.
Read objects from a disk. If the read variable is variant of vartype 8 (string), get reads two bytes first to identify vartype. The next two bytes indicate the word
The length of the string, and then read the string data. The length of the record specified by the Len clause in the open statement must be at least four bytes longer than the actual string length. If the read variable is a dynamic array, get reads a descriptor whose length is equal to 2 plus 8 times the dimension,
2 + 8 * numberofdimensions. Reading the array data and the array descriptor requires occupying the byte, while the Len clause in the open statement specifies
The record length must be greater than or equal to the sum of these bytes. For example, when writing an array to a disk, the following array declaration requires 118 Bytes: dim myarray (1 to 5, 1 to 10) as integer. The allocation of these 118 bytes is as follows: 18 bytes are used for descriptor (2 + 8*2), and 100 bytes are used for data (5*10*2 ). If the read variable is an array of fixed sizes, get reads only data. It does not read the descriptor. If the read variable is any other type of variable (not a variable-length string or variant variable & copy;, get reads only the variable data. Open statement
The length of the record specified by the Len clause must be greater than or equal to the length of the data to be read. When reading user-defined elements, get reads each element separately, but does not fill the elements. On the disk
Before a dynamic array of user-defined types, there is a descriptor whose length is equal to 2 plus 8 multiplied by the dimension, that is, 2 + 8 * numberofdimensions.
The length of the record specified by the Len clause in the open statement must be greater than or equal to the total number of bytes required to read individual elements (including any array and Its descriptor. All random rules apply to files opened in binary mode, except in the following cases: when the Len clause in the open statement does not work, get reads all variables continuously from the disk; that is to say, there is no filling between two records. Get reads only data for any arrays not of the User-Defined type. It does not read the descriptor. Get reads variable-length strings. No matter whether these strings have 2-byte descriptors, they are not user-defined elements. Read
The number of bytes is equal to the number of characters already contained in the string. For example, the following statement reads 10 bytes from a file with file number 1: varstring = string (10,) Get #1, and varstring read dim bit () as byteopen mypicture for binary as #1 redim bit (lof (1) as byte get 1, 1, bit close 1 Write: Open mypicture for binary as #1 Put 1, 1, yourdata close 1dim bit () as byte to meet your requirements, you can read Open mypicture for binary as #1 redim bit (lof (1) as byte get 1, 1, bit close 1dim bit () as byte 'for 40 h and change to ffhfor I = 0 to ubound (BIT) if BIT (I) = 64 then bit (I) = 256 end ifnext 'Finally writes dim bit () as byteopen mypicture for binary as #1 Put 1, 1, bit close 1
Dim bit() As Bytedim file1 as integerdim temp as byteopen "filename" for binary access read write as file1temp=&hffput &h40,tempclose(file1)
dim file1 as integerdim temp as byte file1=freefileopen "filename" for binary access read write as file1temp=&hffput &h40,temp==>put file1,&h40,tempclose(file1)