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.
Old posts that have not been replied to for several years.
T
TomSommer
Post
by TomSommer » Sat Sep 07, 2002 10:23 am
Hello, I want to make a backup of a file called words.txt to words.bak
So I was wondering how to do this... perhaps there is a copy function or something?
Papillon
Owner
Posts: 724 Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no
Post
by Papillon » Sat Sep 07, 2002 10:57 am
open the txt file and read it into a variable/list, then open a new file (the bak file) and put the variable in there.... [open $somefile a+] <--- that should make a new file if it does not already exists.... u might wanna check if the file exists
Elen sila lúmenn' omentielvo
T
TomSommer
Post
by TomSommer » Sat Sep 07, 2002 11:13 am
Thanks.. how do I read the whole file into a variable.. gets only gets one line
Papillon
Owner
Posts: 724 Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no
Post
by Papillon » Sat Sep 07, 2002 11:25 am
I think this:
Code: Select all
set op [open $thefile r]
set fs [read $op]
should put everything in the file into the $fs var
if not...
Code: Select all
while {![eof $file]} {
set f "[gets $file]"
lappend somelist $f
}
then this should read every line of the file and put them into $somelist
Elen sila lúmenn' omentielvo
T
TomSommer
Post
by TomSommer » Sat Sep 07, 2002 11:27 am
nice, thanks... how do I loop through the list? and put it in.. the read thing does not work
Papillon
Owner
Posts: 724 Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no
Post
by Papillon » Sat Sep 07, 2002 11:40 am
u could just put the whole list into the file as it is... if u want to apply each element to the list :
Code: Select all
foreach l $somelist {
puts $file $l
}
this should do the trick
Elen sila lúmenn' omentielvo
T
TomSommer
Post
by TomSommer » Sat Sep 07, 2002 11:43 am
No, because then there would be lots of {}
But I tried putting all data into a string.... but that does not keep the newlines....
So maybe put it all into an array and load the lines into the new file?
so my question is...
array set x {}
how do I just add an element to x...?
Papillon
Owner
Posts: 724 Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no
Post
by Papillon » Sat Sep 07, 2002 11:50 am
u tried this?
Code: Select all
while {![eof $file]} {
set f "[gets $file]"
puts $backupfile $f
}
Elen sila lúmenn' omentielvo
T
TomSommer
Post
by TomSommer » Sat Sep 07, 2002 11:54 am
DOH!!!
Works like a charm.... Thanks
ppslim
Revered One
Posts: 3914 Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England
Post
by ppslim » Mon Sep 09, 2002 5:52 am
You can simply use the "[file copy]" command provided with Tcl.
It's far quicker, and it doesn't given any of the hastle assosiated with {}'s in badly handled strings.