| View previous topic :: View next topic |
| Author |
Message |
darkmare Voice

Joined: 12 Apr 2004 Posts: 12 Location: Beaverton, OR
|
Posted: Mon Aug 08, 2005 6:37 pm Post subject: File Reading (Code to learn file functions); error |
|
|
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: |
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 _________________
Brendan K Callahan Undernet: MaryFinn & PixelCat on #Mary'sPlace | http://3whack.org/brendan/ Theft by Deception DnB music (Released under the CreativeCommons License)
Last edited by darkmare on Mon Aug 08, 2005 6:56 pm; edited 1 time in total |
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Mon Aug 08, 2005 6:51 pm Post subject: |
|
|
You didn't read the first sticky topic in this forum, use [code] tags when showing snippets. _________________ Follow me on GitHub
- Opposing
Public Tcl scripts |
|
| Back to top |
|
 |
darkmare Voice

Joined: 12 Apr 2004 Posts: 12 Location: Beaverton, OR
|
Posted: Mon Aug 08, 2005 6:57 pm Post subject: |
|
|
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. _________________
Brendan K Callahan Undernet: MaryFinn & PixelCat on #Mary'sPlace | http://3whack.org/brendan/ Theft by Deception DnB music (Released under the CreativeCommons License) |
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Mon Aug 08, 2005 7:01 pm Post subject: |
|
|
| darkmare wrote: | Well, I did, but it was over a year ago  |
Hehe, demond's post is less than a week old
Anyway, the first thing I caught was this:
| Code: | | set data [read -nonewline $temp_file] |
you already closed $temp_file, you can't read it. _________________ Follow me on GitHub
- Opposing
Public Tcl scripts |
|
| Back to top |
|
 |
darkmare Voice

Joined: 12 Apr 2004 Posts: 12 Location: Beaverton, OR
|
Posted: Mon Aug 08, 2005 7:04 pm Post subject: |
|
|
I meant, I read all the FAQs and such; I didn't look at dates for any new ones, *that* part is my bad  _________________
Brendan K Callahan Undernet: MaryFinn & PixelCat on #Mary'sPlace | http://3whack.org/brendan/ Theft by Deception DnB music (Released under the CreativeCommons License) |
|
| Back to top |
|
 |
darkmare Voice

Joined: 12 Apr 2004 Posts: 12 Location: Beaverton, OR
|
Posted: Mon Aug 08, 2005 8:09 pm Post subject: |
|
|
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: |
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:
| Quote: |
* 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 _________________
Brendan K Callahan Undernet: MaryFinn & PixelCat on #Mary'sPlace | http://3whack.org/brendan/ Theft by Deception DnB music (Released under the CreativeCommons License) |
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
|
| Back to top |
|
 |
darkmare Voice

Joined: 12 Apr 2004 Posts: 12 Location: Beaverton, OR
|
Posted: Mon Aug 08, 2005 10:26 pm Post subject: |
|
|
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 <  _________________
Brendan K Callahan Undernet: MaryFinn & PixelCat on #Mary'sPlace | http://3whack.org/brendan/ Theft by Deception DnB music (Released under the CreativeCommons License) |
|
| Back to top |
|
 |
demond Revered One

Joined: 12 Jun 2004 Posts: 3073 Location: San Francisco, CA
|
Posted: Tue Aug 09, 2005 3:34 am Post subject: |
|
|
| {} is the last (empty) string which you get when splitting data by \n and your file ends with \n |
|
| Back to top |
|
 |
|