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 file

Old posts that have not been replied to for several years.
Locked
e
ellis

Write to a file

Post by ellis »

how du i write things to a file ?...
i need to make a log script and that preety mutch the only thing i need to know =)
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

Code: Select all

##open the file for writing
set fid [open $fid]
##set the line to put into the file
set line "This line into the file"
##put the line into the file
puts $fid $line
##close the file
catch {close $fid}
Read some tutorial or the TCL manual http://tcl.activestate.com/man/tcl8.4/T ... ntents.htm to get more info about how to open the file for reading/appending... etc
Elen sila lúmenn' omentielvo
e
ellis

Post by ellis »

thx alot mate
e
ellis

Post by ellis »

humm... maby its me that realy stupid and missed somthing u just wrote or u missed out the part were i need to typ in what file i whant to open =P
e
ellis

Post by ellis »

bind PUBM - * pubm:logev
proc pubm:logev ( nick uhost handle channel text) {
set line1 $line2
set line2 $line3
set line3 $line4
set line4 "$nick: $text"
set fid log.txt [open $fid]
set line $line4
puts $fid $line
catch {close $fid}
}


whats wrong ??... this is quite of a guess so dont flame me if i have done a realy "n00b n00b" misstake
M
MC_8
Voice
Posts: 36
Joined: Sat Apr 26, 2003 5:04 pm
Location: Dallas, Texas
Contact:

Post by MC_8 »

Code: Select all

bind pubm - * proc_name
proc proc_name {nick uhost handle channel arg} {
  if {![file exists log_file]} {close [open log_file w]}
  set io [open log_file a]
  puts $io "<$nick@$channel> $arg"
  close $io
} 
Carl M. Gregory - MC_8
http://mc.purehype.net/whois/
e
ellis

Post by ellis »

that works exelent. but how du i make it so that it cuts of the first line if there is more then a sertan number of lines allready, in the log
M
MC_8
Voice
Posts: 36
Joined: Sat Apr 26, 2003 5:04 pm
Location: Dallas, Texas
Contact:

Post by MC_8 »

You can open the file for reading and load it all into memory then use llength to get whatever you need.

Code: Select all

if {![file exists log_file]} {close [open log_file w]} 
set io [open log_file r]
set list ""
while {![eof $io]} {
  gets $io line
  if {[string trim $line] == ""} {continue}
  lappend list $line
}
close $io
lappend list "<$nick@$channel> $arg"
set st [expr [llength $list]-50]; #assuming you want only the last 50.
set list [lrange $list $st end]
set io [open log_file w]
foreach line $list {
  puts $io $line
}
close $io
Carl M. Gregory - MC_8
http://mc.purehype.net/whois/
Locked