问题
How can I write values in files for each turtle ? For example, I have 100 turtles and I would like to write data specific to each turtle in 100 files. For the moment, my code writes data for all turtles in one file .txt:
to write-locations-to-file
file-open "/home/reduan/IBM/outputs.txt"
ask turtles [
file-print (word who " ; " xcor " ; " ycor " ; " color " ; " [pcolor] of patch-here "\r\n" ) ]
end
Thanks in advance for your help.
回答1:
I'm not sure what it is exactly that you are having trouble with, but you can just open a different file for each turtle. In the example below, I used the who
number to generate different file names, but you could use some other method, as long as all file names are unique.
to write-locations-to-files
ask turtles [
file-open (word "/home/reduan/IBM/outputs-" who ".txt")
file-print (word who " ; " xcor " ; " ycor " ; " color " ; " [pcolor] of patch-here "\r\n" )
file-close
]
end
来源:https://stackoverflow.com/questions/23856832/how-to-write-values-in-files-for-each-turtle