问题
I tried all options to create a new line in my output file, but still I get a txt-file with everything behind the previous information. Even with this supersimple code:
globals [file]
to setup clear-all
set file "results\\GA1.txt" if is-string? file
[while [file-exists? file]
[set file replace-item (length file - 5) file "11" ]
file-open file] end
to go tick write-to-file end
to write-to-file file-print (word ticks) FILE-TYPE "\n" file-write 1 file-print (word " " 2 ";") file-write 1 file-print (word " " 2 ";") file-print "" ;; blank line end
I do not get blank lines or line breaks. I work in NetLogo 4.1. Does anybody know what could be the problem?
回答1:
answered at http://netlogo-users.18673.x6.nabble.com/Adding-a-new-line-when-outputting-data-tp4870905p4870909.html where I wrote:
Are you on Windows and using Notepad to view your files? Nearly every other Windows program these days understands Unix-style line breaks, but Notepad doesn't.
回答2:
You forgot to close the file with file-close
or to force it to write data to disk with a file-flush
.
When you do a file-write
the data does not get written to disk immediately. It gets placed in a buffer. When the buffer is large enough the data is written to disk. You can force netlogo to write data to a file by using the file-flush
or file-close
commands.
回答3:
The reference suggests using a better editor. But there is a way to make Notepad work.
If you add the string "\r\n" to your data (with file-type or whatever), it works with Notepad.
Notepad uses the older carriage return ("\r")/ new line ("\n") format. The Unix systems just use new line.
回答4:
Try this, this works for me
file-open "locations.txt"
ask turtles
[file-print xcor]
file-close
if you want more breaks, then use this
file-open "locations.txt"
ask turtles
[file-print xcor file-print "\n"]
file-close
来源:https://stackoverflow.com/questions/3455089/netlogo-line-break-in-output-file-not-so-simple