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.

File Reading (Code to learn file functions); error

Old posts that have not been replied to for several years.
Locked
User avatar
darkmare
Voice
Posts: 12
Joined: Mon Apr 12, 2004 12:55 pm
Location: Beaverton, OR
Contact:

File Reading (Code to learn file functions); error

Post by darkmare »

To learn about playing with files, I put this code in the v2-alpha of my bot. (If you'd like to see examples of some of it's new code, go to http://3whack.org/forums and look all the way at the bottom under PIxelCat Dev Team).

When I try to read the file, it gets all the way through the code, but doesn't spit anything out. Via the telnet, I get this error: "[15:18] Tcl error [act:trigger]: can not find channel named "file14""

What shows in channel is this:
* AuntieChrist Pix file read test.egt (yeah, I'm silly, I made a file extention for EGgdrop Text)
* PixelCatv2 file14
* PixelCatv2 opened file named test.egt
* PixelCatv2 read file named test.egt
* PixelCatv2 closed file named test.egt

For my tutorial, I used this post here: http://forum.egghelp.org/viewtopic.php?t=6885
The append and create work just fine, the reading isn't.

Code: Select all

proc act:trigger {nick mask hand chan keyword arg} {
if {[lindex $arg 0] == "Pix"} {
	global temp_file data counter temp_string file_name
	set temp_file ""
	set data ""
	set temp_string ""
	set file_name [lindex $arg 3]
	if {[lindex $arg 2] == "create"} {
		set temp_file [open $file_name "w+"]
		putserv "PRIVMSG $chan :\001ACTION created file named $file_name\001"
		close $temp_file
		putserv "PRIVMSG $chan :\001ACTION closed file named $file_name\001"
		return
	}
	if {[lindex $arg 2] == "read"} {
		set temp_file [open $file_name "r"]
		putserv "PRIVMSG $chan :\001ACTION $temp_file\001"
		putserv "PRIVMSG $chan :\001ACTION opened file named $file_name\001"
		putserv "PRIVMSG $chan :\001ACTION read file named $file_name\001"
		close $temp_file
		putserv "PRIVMSG $chan :\001ACTION closed file named $file_name\001"
		set data [read -nonewline $temp_file]
		putlog "Debug B"
		set temp_string [split $data "\n"]
		putserv "PRIVMSG $chan :\001ACTION is parsing data. . .\001"
		set counter 0
		while {$counter <= [llength $data]} {
			putserv "PRIVMSG #chan :\001ACTION sees [lindex $temp_string $counter] as line [$counter + 1] in $file_name.\001"
			incr counter
		}
		return
	}
	if {[lindex $arg 2] == "append"} {
	set temp_file [open $file_name "r"]
	putserv "PRIVMSG $chan :\001ACTION opened file named $file_name\001"
	set temp_string ""
		set counter 4
		while {$counter <= [llength $arg]} {
			lappend temp_string [lindex $arg $counter]
			incr counter
		}
		set [lindex $arg 3] [open $file_name "a"]
		puts $temp_file $temp_string
		putserv "PRIVMSG $chan :\001ACTION appended $temp_string to file named $file_name\001"
		close $temp_file
		putserv "PRIVMSG $chan :\001ACTION closed file named $file_name\001"
		return
	}

}
}
Any help would be appreciated,
Brendan
Last edited by darkmare on Mon Aug 08, 2005 6:56 pm, edited 1 time in total.
Image
Brendan K Callahan Undernet: MaryFinn & PixelCat on #Mary'sPlace | http://3whack.org/brendan/ Theft by Deception DnB music (Released under the CreativeCommons License)
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

You didn't read the first sticky topic in this forum, use

Code: Select all

 tags when showing snippets.
User avatar
darkmare
Voice
Posts: 12
Joined: Mon Apr 12, 2004 12:55 pm
Location: Beaverton, OR
Contact:

Post by darkmare »

Well, I did, but it was over a year ago :) I only come here when I've gone over stuff for hours, and no luck, hence the long time between visits. My apologies.
Image
Brendan K Callahan Undernet: MaryFinn & PixelCat on #Mary'sPlace | http://3whack.org/brendan/ Theft by Deception DnB music (Released under the CreativeCommons License)
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

darkmare wrote:Well, I did, but it was over a year ago :)
Hehe, demond's post is less than a week old :P

Anyway, the first thing I caught was this:

Code: Select all

set data [read -nonewline $temp_file]
you already closed $temp_file, you can't read it.
User avatar
darkmare
Voice
Posts: 12
Joined: Mon Apr 12, 2004 12:55 pm
Location: Beaverton, OR
Contact:

Post by darkmare »

I meant, I read all the FAQs and such; I didn't look at dates for any new ones, *that* part is my bad :oops:
Image
Brendan K Callahan Undernet: MaryFinn & PixelCat on #Mary'sPlace | http://3whack.org/brendan/ Theft by Deception DnB music (Released under the CreativeCommons License)
User avatar
darkmare
Voice
Posts: 12
Joined: Mon Apr 12, 2004 12:55 pm
Location: Beaverton, OR
Contact:

Post by darkmare »

OK, we're getting somewhere now. The code that works is as follows: (Mostly so those that come after me see what the fix was)

Code: Select all

proc act:trigger {nick mask hand chan keyword arg} {
if {[lindex $arg 0] == "Pix"} {
	global temp_file data counter temp_string file_name length
	set temp_file ""
	set data ""
	set temp_string ""
	set file_name [lindex $arg 3]
	if {[lindex $arg 2] == "create"} {
		set temp_file [open $file_name "w+"]
		putserv "PRIVMSG $chan :\001ACTION created file named $file_name\001"
		close $temp_file
		putserv "PRIVMSG $chan :\001ACTION closed file named $file_name\001"
		return
	}
	if {[lindex $arg 2] == "read"} {
		set temp_file [open $file_name "r"]
		putserv "PRIVMSG $chan :\001ACTION opened file named $file_name\001"
		putserv "PRIVMSG $chan :\001ACTION read file named $file_name\001"
		set data [read -nonewline $temp_file]
		set temp_string [split $data "\n"]
		set length [llength $temp_string]
		putserv "PRIVMSG $chan :\001ACTION is parsing data. . .\001"
		set counter 0
		while {$counter < $length} {
			putserv "PRIVMSG $chan :\001ACTION sees \"[lindex $temp_string $counter]\" as line $counter in $file_name.\001"
			incr counter
		}
		close $temp_file
		putserv "PRIVMSG $chan :\001ACTION closed file named $file_name\001"
		return
	}
	if {[lindex $arg 2] == "append"} {
	putserv "PRIVMSG $chan :\001ACTION opened file named $file_name\001"
	set temp_string ""
		set counter 4
		while {$counter <= [llength $arg]} {
			lappend temp_string [lindex $arg $counter]
			incr counter
		}
		set temp_file [open $file_name "a"]
		puts $temp_file $temp_string
		putserv "PRIVMSG $chan :\001ACTION appended \"$temp_string\" to file named $file_name\001"
		close $temp_file
		putserv "PRIVMSG $chan :\001ACTION closed file named $file_name\001"
		return
	}

}
}
Now, while not an error per se, I am getting some strange stuff:
* AuntieChrist Pix file append test.egt I Like Blue.
* PixelCatv2 opened file named test.egt
* PixelCatv2 appended "I Like Blue. {}" to file named test.egt
* PixelCatv2 closed file named test.egt
* AuntieChrist Pix file read test.egt
* PixelCatv2 opened file named test.egt
* PixelCatv2 read file named test.egt
* PixelCatv2 is parsing data. . .
* PixelCatv2 sees "Now for something *really* different. {}" as line 0 in test.egt.
* PixelCatv2 sees "Something not so different. {}" as line 1 in test.egt.
* PixelCatv2 sees "I Like Blue. {}" as line 2 in test.egt.
* PixelCatv2 closed file named test.egt
Where are those braces and the space coming from? Is there a way to get rid of them?

And thanks for the help
Image
Brendan K Callahan Undernet: MaryFinn & PixelCat on #Mary'sPlace | http://3whack.org/brendan/ Theft by Deception DnB music (Released under the CreativeCommons License)
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Try [split $arg] instead of $arg.
User avatar
darkmare
Voice
Posts: 12
Joined: Mon Apr 12, 2004 12:55 pm
Location: Beaverton, OR
Contact:

Post by darkmare »

OK, there's two places that could go, though I'm pretty sure you meant the one in the while statement. More importantly to me is *why* is that getting in there? I mean, where is that token coming from? Is this some default behaviour I could override?

OK, I found it. Easier solution is to change the <= to < :)
Image
Brendan K Callahan Undernet: MaryFinn & PixelCat on #Mary'sPlace | http://3whack.org/brendan/ Theft by Deception DnB music (Released under the CreativeCommons License)
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

{} is the last (empty) string which you get when splitting data by \n and your file ends with \n
Locked