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.

Reading and writing to files

Help for those learning Tcl or writing their own scripts.
Post Reply
C
Christmas
Voice
Posts: 2
Joined: Sat Feb 18, 2006 4:24 pm
Location: Bucharest
Contact:

Reading and writing to files

Post by Christmas »

Hy all! This is my first post here, I just downloaded eggdrop and I'm a newbie to TCL scripting. I scripted in mIRC scripting before but TCL brings new and harder topics to understand. Well what I need (and could not find out) is how can I overwrite a line in a file without the need of reading the previous lines or using a temporary file. Here is my code that I have now:

Code: Select all

bind part - * data_seen

proc data_seen {nick uhost hand chan text} {
	set file1 [open "DSotM/Temp/Seen.txt" r]
	set file2 [open "DSotM/Temp/Tmp/Seen.txt" w]
	set host [lindex [split $uhost @] 1]
	set date [clock format [clock seconds] -format "%D"]
	set time [clock format [clock seconds] -format "%T"]
	set contor 0
	while {[gets $file1 line] >= 0 && $contor == 0} {
		if {[lindex $line 0] == $nick} {
			puts $file2 "$line"
		}
	}
	puts $file2 "$nick $host $date $time"
	close $file1
	close $file2
	file copy -force "DSotM/Temp/Tmp/Seen.txt" "DSotM/Temp/Seen.txt"
	file delete "DSotM/Temp/Tmp/Seen.txt"
}
Now this script is opening the original file, $file1, read lines from it and if the nick that parted the channel it's not the nick in the file, it writes that line to a temporary file. Then at the end it appends the nick, host, date and time variables to that temp file and then it overwrites the original file. Now this works for small files, but with files larger than 1000 nicks (and that is very possible for a crowded channel) this proccess of reading and writing takes veeeeery much time. How can insert that $nick $host $date $time at a defined line in the original file, so I won't have 2 lines with the same nick on them?

Thank you in advance, Christmas_.

LE: Sorry for that code formatting, how do I use that code tags? :oops:
The sun is the same in a relative way but you're older
And shorter of breath and one day close to death.
User avatar
Sir_Fz
Revered One
Posts: 3793
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

You can read the file into a list, remove the line you want using [lreplace] and then save back.
C
Christmas
Voice
Posts: 2
Joined: Sat Feb 18, 2006 4:24 pm
Location: Bucharest
Contact:

Post by Christmas »

Well I created a list, each element of it contains a line of the file and if $nick exists in the list, that element gets replaced with the new info for $host, $date and $time, or else the $nick, $host, $date and $time are appended to the list as a new element at the end. Well if I write this list to a file (element by element) it take the same time as before...
The sun is the same in a relative way but you're older
And shorter of breath and one day close to death.
User avatar
Sir_Fz
Revered One
Posts: 3793
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Maybe this might help. See code by user.
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

you just downloaded eggdrop and you are already trying to write nontrivial seen script? wow

I think you are totally confused about the basic principles of saving data from eggdrop scripts; you should load data on script's startup, do all of your data handling in memory and only periodically write to file
connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use

Code: Select all

 tag when posting logs, code
Post Reply