This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Basic File Operations

Help for those learning Tcl or writing their own scripts.
Post Reply
G
Gust
Voice
Posts: 15
Joined: Wed Jun 28, 2006 12:53 pm

Post by Gust »

OK, very old topic but still problems...

With the code for reading a whole file:

Code: Select all

set fname "textfile.txt"
set fp [open $fname "r"]
set data [read -nonewline $fp] 
close $fp
set lines [split $data "\n"]
my bot just reads the first line...

How to solve that?
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

You sure it only read 1 line? How are you trying to determine what's in the $data var after reading in the file?

If you do:

Code: Select all

foreach line [split $data \n] {
                putcmdlog "line '$line'"
}
you should see all of the data and some blank lines?

When I read/write to files, I check for blank lines and exclude them along the way:

Code: Select all

# read data in

if {![file exists $quotefile]} {return "No quotes available."}
set quotelines ""
set inqfile [open $quotefile r]
set quotetemp [split [read $inqfile] \n]
catch {close $inqfile}

foreach line $quotetemp {
	if {$line != ""} {
			lappend quotelines $line
	}
}

Code: Select all

# write data out

set quotewrite [open $quotefile w]
foreach line $quotelines {
	set line [string trim $line]
	if {$line != ""} {
		puts $quotewrite $line
	}
}
catch {close $quotewrite}
Post Reply