[VB learning] 6: file operation

xiaoxiao2021-03-06  40

Opening and reading writing

Open "File Name" for mode AS [#] file number [len = record length]

mode:

(1) Sequential access mode

OUTPUT: Opens a file to write the file.

INPUT: Opens a file, read the file.

Append: Opens a file and add a record at the end of the file.

----- Write data: [Output List] Generally refers to a comma, separated value or string expression. Write # is basically the same as the Print #, the difference is that Write # is stored in a compact format, inserts a comma between the data, and adds double quotes to the string.

Print # file number, [Output list]

File Print_File.txt write format:

Miracle oh banana

Write # file number, [Output list]

File Write_File.txt Write Format: "Miracle", "Oh", "Banana"

----- Read the data:

INPUT # file number, variable list

Document print_file.txt and write_file.txt read format:

Miracle oh banana

LINE INPUT # file number, string variable

File Print_File.txt Read Format:

Miracle oh banana

File Write_File.txt Read Format: "Miracle", "Oh", "Banana"

INPUT $ (read the number of characters, # file number)

Format with line Input #

Two functions related to reading files:

LOF (): Returns the number of bytes of a file.

EOF (): Check that the pointer reaches the end of the file.

(2) Random access mode

Random: File After opening a random manner, you can write and read operation simultaneously, but you need to indicate the length of the record, the system default length is 208 bytes.

----- Write data: The get command reads a record content specified by the record number in the disk file. The record number is an integer greater than 1, indicating that the number record is operated, if ignored Write, the next record of the current record.

Get [#] file number, [record number], variable name

File get_file.txt write format: miracle oh banana

----- Read Data: The command is the record position specified by the content of the record variable, writes the open disk file; the record number is an integer greater than 1, indicating the sum of the quadch records, if not ignored Write, then insert a record after the current record.

PUT [#] file number, [record number], variable name

File get_file.txt Read the format (displays the record number, such as 3 is):

banana

(3) Binary access mode

Binary: The most original file type, store the binary code in the file, no format, to locate the data, allow the program to organize and access data as needed, also allow the bytes of the files Data is accessed and accessed.

File number:

The file number is an integer between 1-511. When you open a file, you need to specify a file number. This file number represents the file until this number can be used by other files until the file is closed. You can use the freefile () function to get the next file number that can be utilized. DIM LNGFH AS Long

LNGFH = freefile

File close

Close [#] file number [, [#] file number] ...

After the various read and write operations, the file must be turned off, otherwise the data is lost. The command to close the file is Close.

转载请注明原文地址:https://www.9cbs.com/read-75924.html

New Post(0)