A file operation command is provided in Rational Robot, the syntax is as follows:
Open filename $ [for mode] [Access Access] [LOCK] AS [#] filenumber% [len = reclen]
Here we divide its grammar into two parts, because this file operation command has two file operation mode, one is the sequential file, one is a random file.
The following is a syntax for the order file operation:
Open filename $ [?? for [INPUT | OUTPUT | APPEND] AS [#] filenumber [len = buffersize]
Parameter Description:
Description:
(1) Parameter filename $ indicates that the file name to be opened, the file name can contain drives and directories
(2) INPUT OUTPUT and APPEND are used to set open methods for sequential files. Wherein, INPUT represents reading data from the open file. When you open a file in this way, the file must exist, otherwise an error will occur. Output indicates that data is written to the open file. When the file is opened in this way, the original data in the file will be overwritten, and the new data will be written from the file. If the file does not exist, create a new file. Append means adding data to the open file. When opening this way, the original data in the file will be retained, and the new data will be added from the file. If the file does not exist, create a new file.
(3) AS [#] filenumber clause is used to specify the file number for the open file. When reading / writing, the file number is used to indicate the file. The file number is an integer between 1 ~ 511, both Can be numbers, but also variables. It can also be omitted.
(4) When data is copied between files and programs, the LEN = Buffersize clause specifies the number of characters in the buffer.
example:
Open "c: /test.dat" for Output As 1
Open "c: /test.dat" for Output As 1
This two sentences created a text file called Test.dat in the directory where the C drive is located, and the file number is 1.
Open "c: /test.dat" ?? for input as [#] filenumber This statement is to read data from a text file.
Open app.path "/test.dat" for append as [#] filenumber ?? This statement is to add data to a text file.
Random file operation:
Before operating a random file, you must first define a record type for saving data items. This record is a user-defined data type, which is the basic structure of storage data in a random file. For example:
Type Student
No as integer
Name as string * 20
AGE AS INTEGER
End Type
DIM STUD AS Student 'Defines a variable that can be stored
In a random file, all data will be saved to a number of STUDENT types, while the data read from the random file can be stored in the variable STUD. After we open and read and write files.
Operating syntax format of a random file:
Open filename for random as [#] filenumber len = reclength
Description:
(1) Parameters FileName and FileNumber represent file names or file numbers, respectively.
(2) Keyword Random means that the random file is open
(3) The LEN clause is used to set the recording length, and the length is specified by the parameter ReClength.Reclength must be greater than 0, and must be consistent with the length of the defined recording structure. The method of calculating the recording length is to make each element in the recording structure. The length is added. For example, the length of the STUDENT that the previously declared should be 2 20 2 = 24 bytes.
Open a random file for a record type to Student is: Open "c: /student.txt" for random as # 1 gs = 25
There is also a file operation method binary, and the lower side is his grammar format:
Open pathname for binary as [#] filenumber
Description:
(1) Parameters FileName and FileNumber represent file names or file numbers, respectively.
(2) Keyword binary means that the binary file is open
(3) For binary files, the byte length cannot be specified. Each open binary has a pointer, the file pointer is a numeric value, pointing to the location of the next read and write operation. Each binary "Location" corresponds to a data byte, so there is a file with n bytes, there is 1 to n position.
We can return the current file pointer position with the seek () function (the next byte to read and write); use the LOC () function to return the last read-write byte location unless the pointer is moved with the SEEK statement, LOC ( The return value is always less than Seek () small 1. Let's look at the example below:
Open "Path: /student.txt" for binary as # 1? This statement opened the Student.txt file in a binary manner.