QB64 中冲突的 FIELD 语句

Conflicting FIELD statements in QB64

当运行这段代码好像testfile.000包含“00”而testfile.001包含“99”,那么,为什么这些文件之间没有冲突??它应该抛出 "duplicate definition." 吗?

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

在您的代码中,第二个 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