检查文件是否已更改并仅打印新数据的有效方法是什么?
最初,我倾向于比较行数的 readline 方法,但这似乎非常低效。
我最初的查询是是否有办法从 Netlogo 检查文件大小或时间戳。答案是“不”。然而,Charles Staelin 很友好地将 get-date-ms 方法添加到 pathdir 扩展中。因此,现在可以检查文件更改(根据时间戳)set currenttimestamp pathdir:get-date-ms "somefile.nlist"
if currenttimestamp != filetimestamp
使用添加的方法:
to checkfile
ifelse checktick mod 19 = 0
[set currenttimestamp pathdir:get-date-ms "somefile.nlist"
if currenttimestamp != filetimestamp
[file-open "somefile.nlist"
let thiscount 0
while [not file-at-end?][
set in1 file-read-line
set global-in1 in1
set thiscount thiscount + 1
if thiscount > global-filelength AND (not file-at-end?)[ ;it seems that this not-at-file-end is redundant - why not?
print in1
set global-filelength thiscount
set hasfilechanged true
]
]
file-close
set filetimestamp currenttimestamp
set checktick checktick + 1
]
]
[ set checktick checktick + 1]
end