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.

Explanation on File Reading?

Help for those learning Tcl or writing their own scripts.
Post Reply
d
dokueki
Voice
Posts: 31
Joined: Sat Apr 28, 2007 8:42 am
Location: Bat Yam, Israel
Contact:

Explanation on File Reading?

Post by dokueki »

Hi there, all.
I'm trying to go for a bit more dynamic scripts which are managing files and withdraws information from them. Although, I just can't seem to really understand how file managing works in Tcl. If anyone would be willing to help, it'd be great! What I need to know is:
  1. When do I need to open/close files? Let's say I want a file to be accessed whenever the bind is activated.
  2. How do I tell it to access specific lines in a file?
  3. How do I write one line to the last line in the file?
Thanks in advance!
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

If you have questions after reading the FAQ forum, specifically the faq dealing with reading/writing files, let us know.
d
dokueki
Voice
Posts: 31
Joined: Sat Apr 28, 2007 8:42 am
Location: Bat Yam, Israel
Contact:

Post by dokueki »

Alright, I'm sorry I didn't check there :C I'll try what the topic there says and I'm pretty sure it'll work. One thing though, how can I pick a random file from a list of files in a directory? Is that possible? Thanks again in advance :3
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

To open a random (already existing) file, I'd suggest you first get a list of files using glob, get the lenght of the list using llength, and generate a random number between 0 and length-1 (using rand) to be used with lindex to retrieve the randomly selected file.

Code: Select all

set files [glob "mydir/*"]
set file [lindex $files [rand [llength $files]]]
Once you have the filename, perform your file operations just as you would with any other file.
NML_375
d
dokueki
Voice
Posts: 31
Joined: Sat Apr 28, 2007 8:42 am
Location: Bat Yam, Israel
Contact:

Post by dokueki »

Alright, thanks again! Works like a charm :3
Post Reply