| View previous topic :: View next topic |
| Author |
Message |
sdays Halfop
Joined: 21 Oct 2006 Posts: 98
|
Posted: Thu Apr 19, 2007 9:07 pm Post subject: write to a text file.. |
|
|
when my bot add a line to a text file i need it to add a num like below.
like:
|
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
|
| Back to top |
|
 |
sdays Halfop
Joined: 21 Oct 2006 Posts: 98
|
Posted: Thu Apr 19, 2007 9:19 pm Post subject: |
|
|
| Code: |
proc addtemp {nick uhost} {
global tempfile file
set file [open $tempfile a]
puts $file "$nick $uhost"
close $file
return 0
}
|
|
|
| Back to top |
|
 |
sdays Halfop
Joined: 21 Oct 2006 Posts: 98
|
Posted: Fri Apr 20, 2007 7:49 pm Post subject: |
|
|
| i need it to count the line before adding ,like add 1 then the line_here |
|
| Back to top |
|
 |
sdays Halfop
Joined: 21 Oct 2006 Posts: 98
|
Posted: Sat Apr 21, 2007 5:58 am Post subject: |
|
|
ok heres what i get now "<eggdrop> Tcl error [temp_add]: channel "file12" wasn't opened for reading
| Code: |
set temp "templist.txt"
proc addtemp {} {
global file linecount temp nick
set addtemp_file [open $temp w]
set linecount 0
set lines [split [read $addtemp_file] \n]
foreach line $lines {
incr linecount
}
puts $addtemp_file "$linecount $nick"
close $addtemp_file
return 0
}
|
|
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sat Apr 21, 2007 7:34 am Post subject: |
|
|
You're opening the file in write-only mode. Check your "access" argument for your open-command. Best approach would be to first open your file as readonly, read all the data, close it, reopen it as write only (with truncation) and write the modified data. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
sdays Halfop
Joined: 21 Oct 2006 Posts: 98
|
Posted: Sat Apr 21, 2007 4:43 pm Post subject: |
|
|
i done that now other bug..
heres tcl:
| Code: |
proc addtemp {} {
global linecount temp
set addtemp_file [open $temp a+]
set lines [split $addtemp_file "\n"]
foreach line $lines {
incr linecount
}
set linecount 0
puts $addtemp_file "$linecount $nick"
close $addtemp_file
return 0
}
|
here's text file:
|
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sat Apr 21, 2007 6:41 pm Post subject: |
|
|
It would seem you reset linecount to 0 after you've read all the lines in the file. Also, rather than using foreach to increase the count, you might considder using llength (since you already split it to a list) instead. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Sat Apr 21, 2007 7:43 pm Post subject: |
|
|
+ You're not reading your file. Try
| Code: | proc addtemp {} {
global temp
set addtemp_file [open $temp a+]
set lines [split [read $addtemp_file] \n]
set linecount [llength $lines]
puts $addtemp_file "[expr {$linecount+1}] $nick"
close $addtemp_file
return 0
} |
_________________ Follow me on GitHub
- Opposing
Public Tcl scripts |
|
| Back to top |
|
 |
sdays Halfop
Joined: 21 Oct 2006 Posts: 98
|
Posted: Sat Apr 21, 2007 7:55 pm Post subject: |
|
|
still samething..
|
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Sat Apr 21, 2007 9:20 pm Post subject: |
|
|
I doubt you got any result at all because $nick does not exist. Perhaps this is better:
| Code: | proc addtemp nick {
global temp
set addtemp_file [open $temp r+]
set linecount [llength [split [read $addtemp_file] \n]]
puts $addtemp_file "$linecount $nick"
close $addtemp_file
return 0
} |
usage: addtemp <nick> (this is supposed to continue the count, so we assume that the pevious nicks are already numbered in the file). _________________ Follow me on GitHub
- Opposing
Public Tcl scripts |
|
| Back to top |
|
 |
|