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.

write to a text file..

Help for those learning Tcl or writing their own scripts.
Post Reply
s
sdays
Halfop
Posts: 98
Joined: Sat Oct 21, 2006 4:46 am

write to a text file..

Post by sdays »

when my bot add a line to a text file i need it to add a num like below.

like:

Code: Select all

1 line
2 line
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Show us your code.
s
sdays
Halfop
Posts: 98
Joined: Sat Oct 21, 2006 4:46 am

Post by sdays »

Code: Select all

proc addtemp {nick uhost} {
  global tempfile file 
  set file [open $tempfile a]
  puts $file "$nick $uhost"
  close $file
  return 0
}
s
sdays
Halfop
Posts: 98
Joined: Sat Oct 21, 2006 4:46 am

Post by sdays »

i need it to count the line before adding ,like add 1 then the line_here
s
sdays
Halfop
Posts: 98
Joined: Sat Oct 21, 2006 4:46 am

Post by sdays »

ok heres what i get now "<eggdrop> Tcl error [temp_add]: channel "file12" wasn't opened for reading

Code: Select all

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
}

n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

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
s
sdays
Halfop
Posts: 98
Joined: Sat Oct 21, 2006 4:46 am

Post by sdays »

i done that now other bug..

heres tcl:

Code: Select all

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:

Code: Select all

0 lomax
0 evil
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

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
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

+ You're not reading your file. Try

Code: Select all

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
}
s
sdays
Halfop
Posts: 98
Joined: Sat Oct 21, 2006 4:46 am

Post by sdays »

still samething..

Code: Select all

1 lomax
1 evil
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

I doubt you got any result at all because $nick does not exist. Perhaps this is better:

Code: Select all

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).
Post Reply