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.

Saving a variable into a text file

Old posts that have not been replied to for several years.
Locked
R
Real
Voice
Posts: 24
Joined: Mon Mar 08, 2004 11:27 am

Saving a variable into a text file

Post by Real »

Well I think the topic says it all doesn't it :)

I've been trying to make the script myself, but it's just too hard for me :(
I've also searched on the forum, but nothing seems to work

So first, I want to write a line in a text file:
proc savevar
set thevar $text
save $thevar in $thisfile (so there should be just 1 line in the text file, this means replacing an other line :) )

Second:
I want to make my bot read the file and save the line into a var on every load.

How can this be done? :/

Thank you very much
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

You can do something like this.

Code: Select all

set myfile "data.txt"
set myvar "my data here"

#To write the variable to the file.
set file [open $myfile "a"]
puts $file "$myvar"
close $file

#To read the variable
set file [open $myfile "r"]
set newvar [gets $file]
close $file
Try reading the FAQ section, with the topic 'BASIC FILE OPERATIONS' in this forum on the other section. It has very useful tips regarding this.

Anyway, in this case, the data will be written in the first line of the file, and will be appended and overwritten each time (will be replaced), and it will be read from the first line as well.

I assume you can do the rest, make a procedure and use these codes in your procedure accordingly.
Last edited by awyeah on Tue Jul 13, 2004 9:28 pm, edited 1 time in total.
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

Here's the code I use to do a backup of a array called 'settings'

Code: Select all

set settings_file "settings.dat"

proc backup_settings {} {
 global settings settings_file
 if {![catch {open $settings_file w} fileid]} {
   puts $fileid "### Channel Settings ###"
   foreach w [array names settings] {
     set setting "[lindex [split [array get settings $w]] 1]"
     puts $fileid "set settings($w) $setting"
   }
   puts $fileid "### EOF Channel Settings ###"
   close $fileid
   return 1
  } else {
   putlog "Error: Could not save settings to $settings_file"
   return 0
 }
}
Then you can just load it on startup like you normally load a tcl, with

Code: Select all

source settings.dat
Locked