QB64中的FIELD语句冲突

时间:2019-05-08 07:15:28

标签: qb64

运行此代码时,似乎testfile.000包含“ 00”,而testfile.001包含“ 99”,因此,为什么这些文件之间没有冲突?它应该抛出“重复定义”吗?

file1$ = "testfile.000"
file2$ = "testfile.001"
OPEN file1$ FOR RANDOM AS #1 LEN = 2
FIELD #1, 2 AS x$
OPEN file2$ FOR RANDOM AS #2 LEN = 2
FIELD #2, 2 AS x$
LSET x$ = "99"
PUT 1, 1
PUT 2, 1
END

1 个答案:

答案 0 :(得分:1)

在您的代码中,第二个FIELD声明将覆盖第一个FIELD声明。在BASIC中,FIELD语句中声明的变量应该是不同的。

如果使用下面的代码,则会看到有趣的结果:

文件testfile.000将包含1212(仅第一个LSET设置文件#1的字段x $)

文件testfile.001将包含1488(LSET都设置了文件#2的字段x $)。

file1$ = "testfile.000"
file2$ = "testfile.001"
OPEN file1$ FOR RANDOM AS #1 LEN = 2
FIELD #1, 2 AS x$
LSET x$ = "12"

OPEN file2$ FOR RANDOM AS #2 LEN = 2
FIELD #2, 2 AS x$
LSET x$ = "14"

PUT #1, 1: REM This puts 12 into the 1st file#1 record.
PUT #2, 1

LSET x$ = "88"
PUT #1, 2: REM this puts 12 into the 2nd file#1 record
PUT #2, 2

CLOSE #1
CLOSE #2
END

在QB中,您可以按照以下代码使用二进制文件。此模式允许您在多个文件中写入相同的变量。

使用这种文件时,您将完全控制数据在文件中的尺寸和位置。

file1$ = "testfile.000"
file2$ = "testfile.001"
OPEN file1$ FOR BINARY AS #1
OPEN file2$ FOR BINARY AS #2

x$ = "29"
PUT #1, 1, x$
PUT #2, 1, x$

CLOSE #1
CLOSE #2
END