NetLogo: line break in output file?? Not so simple. - file

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?

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.

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.

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.

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

Related

Writing file in Tcl gives extra line

I am wondering from where newline (4th in example code) is written out from following very simple tcl code. Handling from puts -nonewline is cumbersome. Is there any other tcl command influence this behavior?
set fid [open testout.txt w]
puts $fid 1
puts $fid 2
puts $fid 3
close $fid
Output:
#1:1
#2:2
#3:3
#4:
The puts command always appends a newline to the end of what you ask it to write, unless you pass the -nonewline option. It is a feature of that command, and is most of the time what you tend to want. (The puts command is the only standard Tcl command that writes data to a channel; chan puts is just a different name for the same thing.)
In your case, maybe you don't want the newline at the end of the final line (and should use the option). Or maybe you want to trim the newline from the end before splitting the text into lines when reading it back in. Whether you can tolerate that newline character at the end of the text data in the file depends on what you're doing with it.

In a .bat can text variable content be used in place of text file with WinMsg.exe?

In the batch file I am writing which suffices for my need, I'm calling an app (WinMsg.exe) that creates a messagebox using text input. It offers two input switches, -m for text quoted or in a named variable containing text, and -f to read input from a text file, which can be taken from a named variable. The -m limit is 255 chrs while the -f limit is longer. The text I want as input is >255 chrs and is stored in one or more variables. I can use multiple variables with -m but it breaks when the total exceeds 255 chrs. So it appears -f will have to be used. My question is, if -f is expecting a filename, is there a way to "disguise" or somehow redirect my text string variables to be read as if they were actually contained in a file, and without first writing the variable content to a file? I've not seen any questions with quite the same objective and I have doubts that it is even possible but I want to be sure.
There is absolutely no need to think about your SSD. A high end SSD with plenty of free space in a home computer lasts centuries.
This reads from StdIn by line and displays in a Msgbox
Set Inp = WScript.Stdin
Do Until Inp.AtEndOfStream
MsgBox Inp.Readline
Loop
Or the whole file
Set Inp = WScript.Stdin
MsgBox Inp.ReadAll
To use
Cscript //nologo "C:\folder\file.vbs" < "c:\windows\win.ini"

Using TCL is it possible to read a file "backwards"

I have a very large text file from which I have to extract some data. I read the file line by line and look for keywords. As I know that the keywords I am looking for are much closer to the end of the file than to the beginning, I wonder if it is possible to read the file starting at the last row instead of the first. I then would use an aditional keyword which indicates "everything beyound this word is not of interesst" and stop reading.
Is that possible ?
I don't know how performant this would be, but run the file through tac and read from that:
set fh [open "|tac filename"]
# read from last line to first
while {[gets $fh line] != -1} {...
Another tactic would be to read the last, say, 5000 bytes of the file (using seek), split on newlines and examine those lines, then seek to position 10000 from the end and read the "next" 5000 bytes, etc.
No it is not possible (in any runtime/language I'm aware of, Tcl included).
So decide on a buffer side and read your file by seeking backwards and trying to read a full buffer each time.
Note that you have to observe certain possibilities:
The file might be smaller than the size of your buffer.
It seems you're dealing with a text file, and you want to process it line-wise. If so, observe that if the code is cross-platform or has to work on Windows you have to deal with the case when the data placed in the buffer by the last read operation starts with LF, and the next read operation—of the preceding chunk—will end with CR—that is, your EOL marker will be split across the buffers.
You might want to take a look at the implementation of Tcl_GetsObj() in the generic/tclIO.c file in the Tcl source code—it deals with split CRLFs on normal ("forward") reading of a textual string from a file.
The simplest way to grab the end of a file for searching, assuming you don't know the size of the records (i.e., the line lengths) is to grab too much and work with that.
set f [open $filename]
# Pick some large value; the more you read, the slower
seek $f -100000 end
# Read to the end, split into lines and *DISCARD FIRST*
set lines [lrange [split [read $f] "\n"] 1 end]
Now you can search with lsearch. (Note that you won't know exactly where in the file your matched line is; if you need that, you have to do quite a lot more work.)
if {[lsearch -glob $lines "*FooBar*"] >= 0} {
...
}
The discarding of the first line from the read section is because you're probably starting reading half way through a line; dropping the first “line” will mean that you've only got genuine lines to deal with. (100kB isn't very much for any modern computer system to search through, but you may be able to constrain it further. It depends on the details of the data.)
package require struct::list
set fp [open "filename.txt"]
set lines [split [read -nonewline $fp] "\n"]
foreach line [struct::list reverse $lines] {
...
}
do something with "$line".
to reverse file , I read the file into a variable "list" line by line pre-pending $list with the current line. That way List is in reverse order of file ..
while {[gets $in line] > -1} {
if [regexp "#" $line] {
continue
}
# reverse the order in variable "list"
set list "$line $list"
}
foreach line $list {
puts "line:$ln line= $line"
""*** process each line as you need ***""
}

How to read from a specific line from a text file in VHDL

I am doing a program in VHDL to read and write data. My program has to read data from a line, process it, and then save the new value in the old position. My code is somewhat like:
WRITE_FILE: process (CLK)
variable VEC_LINE : line;
file VEC_FILE : text is out "results";
begin
if CLK='0' then
write (VEC_LINE, OUT_DATA);
writeline (VEC_FILE, VEC_LINE);
end if;
end process WRITE_FILE;
If I want to read line 15, how can I specify that? Then I want to clear line 15 and have to write a new data there. The LINE is of access type, will it accept integer values?
Russell's answer - using two files - is the answer.
There isn't a good way to find the 15th line (seek) but for VHDL's purpose, reading and discarding the first 14 lines is perfectly adequate. Just wrap it in a procedure named "seek" and carry on!
If you're on the 17th line already, you can't seek backwards, or rewind to the beginning. What you can do is flush the output file (save the open line, copy the rest of the input file to it, close both files and reopen them. Naturally, this requires VHDL-93 not VHDL-87 syntax for file operations). Just wrap that in a procedure called "rewind", and carry on!
Keep track of the current line number, and now you can seek to line 15, wherever you are.
It's not pretty and it's not fast, but it'll work just fine. And that's good enough for VHDL's purposes.
In other words you can write a text editor in VHDL if you must, (ignoring the problem of interactive input, though reading stdin should work) but there are much better languages for the job. One of them even looks a lot like an object-oriented VHDL...
Use 2 files, an input file and an output file.
file_open(vectors, "stimulus/input_vectors.txt", read_mode);
file_open(results, "stimulus/output_results.txt", write_mode);
while not endfile(vectors) loop
readline(vectors, iline);
read(iline, a_in);
etc for all your input data...
write(oline, <output data>
end loop;
file_close(vectors);
file_close(results);

Read textfile into list in Applescript

I tried to create an AppleScript that reads a text file and puts the contents into a list. The file is a simple text file where every line looks like this: example-"example"
The first is a filename and the other is a folder name.
Here is my code now:
set listOfShows to {}
set theFile to readFile("/Users/anders/Desktop/test.txt")
set Shows to read theFile using delimiter return
repeat with nextLine in Shows
if length of nextLine is greater than 0 then
copy nextLine to the end of listOfShows
end if
end repeat
choose from list listOfShows
on readFile(unixPath)
set foo to (open for access (POSIX file unixPath))
set txt to (read foo for (get eof foo))
close access foo
return txt
end readFile
When I run that the output I get this:
error "Can not change \"Game.of.Thrones-\\\"Game Of \" to type file." number -1700 from "Game.of.Thrones-\"Game Of " to file"
My list looks like this: Game.of.Thrones-"Game Of Thrones" and two more lines like that.
The error is that you are trying to read the contents of a file (the first file you read) as a file. Getting the paragraphs of text will break it apart at return/linefeed boundaries, which usually works better than trying to guess what end of line character(s) were used in the file.
You also don't need the whole open for access thing when just reading files, so your script can be reduced to just
set listOfShows to {}
set Shows to paragraphs of (read POSIX file "/Users/anders/Desktop/test.txt")
repeat with nextLine in Shows
if length of nextLine is greater than 0 then
copy nextLine to the end of listOfShows
end if
end repeat
choose from list listOfShows
read uses MacRoman by default, so it jumbles up non-ASCII characters in UTF-8 files unless you add as «class utf8». (as Unicode text is UTF-16.)
paragraphs of (read POSIX file "/tmp/test.txt") as «class utf8»
paragraphs of also works with CRLF and CR line endings. This doesn't, but it ignores the last line if it's empty:
read POSIX file "/tmp/test.txt" as «class utf8» using delimiter linefeed
set milefile to ((path to desktop as text) & "Alert.txt")
set theFileContents to (read file milefile)
display dialog theFileContents
AppleScript’s Language Reference states on page 120:
A series of characters beginning immediately after either the first character after the end of the preceding paragraph or the beginning of the text and ending with either a carriage return character (\r), a linefeed character (\n), a return/linefeed pair (\r\n), or the end of the text. The Unicode "paragraph separator" character (U+2029) is not supported.
So, U+8232 is ignored and AppleScript returns the whole text from the file…
U+8232 is used in TextEdit as the CR character…

Resources