为什么我无法从net徽标中读取txt文件中的字符串?

时间:2015-01-27 16:36:43

标签: file text netlogo

我试图从网络徽标中读取txt文件中的以下行:

job1 1 1 15 25 90 3 1111 1100 0010 0110 1011 0 0 0 0 0 0 0 0 0 0 0

然而,我收到的所有时间:预期一个常数(第1行,第5个字符)

在这种情况下,我有很多问题。

A)我如何让netlogo读取字符串" Job1" ?

B)考虑到第10个数字是二进制数,我怎样才能成为一个字符串而不是一个数字呢?

感谢您的回答。

Gorillaz Fan

1 个答案:

答案 0 :(得分:2)

我不太确定我是否真的有,你想要实现什么。你想阅读" txt文件"的所有元素吗?作为字符串,但由白色空间分隔? 如果是,您可以尝试逐个字符地读取文件以检查空格之间的字符串长度。然后再次浏览文件并提取这些字符串。这是一个如何实现它的示例代码。也许有更优雅的版本,但这个适合我:

globals
[
  char-list
  char-counter
  string-list
  current-char
]

to read

set char-counter 0
set char-list []
set string-list []
set current-char 0

;; Open the file and go through it, char by char to check where the blank spaces are
file-open "in.txt"

while [file-at-end? = false]
[
  ;; Save current char
  set current-char file-read-characters 1

  ;; Check if the char is a blank space...
  ifelse (current-char != " ")
    ;; If not, increase the length of the current string
    [
      set char-counter char-counter + 1
    ]
    ;; If yes, save the length of the previous string, and reset the char-counter
    [
      set char-list lput char-counter char-list
      set char-counter 0
    ]
]

file-close  

;; Now read the file again and extract only the strings which are not blank spaces
file-open "in.txt"

let i 0
while [i < length char-list]
[
  ;; Read the next number of characters as defined by the previously created char-list
  set string-list lput file-read-characters item i char-list string-list

  ;; Skip 1 space:
  set current-char file-read-characters 1

  ;; Increase i
  set i i + 1
]

file-close

end