1. Reading all the lines from a file into a list.
This is one of the most basic things you can do. It's the basis for several other operations we'll be discussing, so get familiar with it!
Code: Select all
# File name to read.
set fname "yourfile.txt"
# Open file for read access (note we're not catching errors, you might
# want to use catch {} if the file might not exist.
set fp [open $fname "r"]
# Here we read in all of the data.
set data [read -nonewline $fp]
# Close the file, since we're done reading.
close $fp
# Now we split the data into lines.
set lines [split $data "\n"]
2. Reading a random line from a (small) file.
There are lots of reasons you might want to do this: quote scripts, random topic scripts, trivia games, etc. In this example we'll only be reading a single line, but you could easily change it to read more than 1 line in a row, or multiple lines from different places.
Code: Select all
# Use the code from above (1.) to read in all the lines from the file.
# We continue right after: set lines [split $data "\n"]
# Get the number of lines.
set numlines [llength $lines]
# Choose a random line with eggdrop's rand function.
set num [rand $numlines]
# Get the line from the list!
set randline [lindex $lines $num]
3. Deleting a line from a text file.
We'll assume you want to delete a specific line. Remember that most languages, including tcl, index things starting from 0. So if you want to delete the first line, it is line 0. The second line is line 1. Etc.
Code: Select all
# Use the code from above (1.) to read in all the lines from the file.
# We continue right after: set lines [split $data "\n"]
# We'll delete the first line.
set line_to_delete 0
# If you wanted to delete the last line instead, you would do this:
# set line_to_delete [expr [llength $lines] - 1]
# Now, we remove the line from the list in memory first.
set lines [lreplace $lines $line_to_delete $line_to_delete]
# And finally, we re-write the file with the new data.
set fp [open $fp "w"]
puts $fp [join $lines "\n"]
close $fp
4. Inserting a line into a file.
Usually people want to insert lines at the beginning of a file, or append them onto the end. The latter will be handled in the next question.
Code: Select all
# Use the code from above (1.) to read in all the lines from the file.
# We continue right after: set lines [split $data "\n"]
# For the beginning, use line 0, since tcl starts counting at 0. If you want
# to append the line to the end, see the next question for a better way.
set line_to_insert "this is the new line"
set where_to_insert 0
# Now we insert the line into our list in memory.
set lines [linsert $lines $where_to_insert $line_to_insert]
# And now we re-write the file, just like in 3.
set fp [open $fp "w"]
puts $fp [join $lines "\n"]
close $fp
For the special case of adding a line to the end of the file, we don't need
to mess with reading/re-writing at all. We simply open the file in a special
mode called "append mode."
Code: Select all
# The new line.
set line_to_add "the dragon will eat your sheep!"
# Name of file to append to.
set fname "yourfile.txt"
# Open the file in append mode.
set fp [open $fname "a"]
# Add the line.
puts $fp $line_to_add
# We're done!
close $fp
If you'd like to see some other file i/o topics answered here, make a post below!
Of course, don't bother with things like "how do I read a random line and display it in a private message" because the whole reading-random-lines topic is covered already.