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.

Writing, reading and deleting data from a file

Help for those learning Tcl or writing their own scripts.
Post Reply
F
Fill
Halfop
Posts: 80
Joined: Sun Jan 18, 2009 6:08 pm

Writing, reading and deleting data from a file

Post by Fill »

Hi,

I'm making a script that requires writing, reading and deleting entries from a file. With the help of http://wiki.tcl.tk/367, I was able to make the bot write to a file and read it. However, I don't know how to delete a certain entry.

I'm using this to write to the file:

Code: Select all

 set data [lrange $text 1 end]
 set fileId [open Triggers/triggers.$chan "w"]
 puts -nonewline $fileId $data
 close $fileId
How can I make the bot remove a line from the file?

Thanks in advance,
Fill
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Simply put, you can't.

But... what you can do, is read the whole file into memory, and manipulate the content there. Then you either remove the old file and create a new (empty) one, or reopen the file with the TRUNC option (truncates, clears, the file). Finally, you then write your altered data there.

Rough example illustrating the process. No actual modification of the data is done here, and no assumptions as to the layout of the data are made.

Code: Select all

set fid [open "myfile" "RDONLY"]
set data [read $fid]
close $fid

#manipulate $data

set fid [open "myfile" "WRONLY CREAT TRUNC"]
puts $fid $data
close $fid
NML_375
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

I tend to prefer to keep a complete copy of a file's contents in memory and write it whenever a change is made. Perhaps something like the following code :-

Code: Select all

# call this proc to read the file whatever.txt
proc pReadFile {} {
    global vFileData
    if {[file exists whatever.txt]} {
        set id [open whatever.txt r]
        set vFileData [split [read -nonewline $id] \n]
        close $id
    } else {
        # handle file doesn't exist
    }
    return 0
}
     
# call this proc to write the file whatever.txt
# the file is first created if it doesn't exist or truncated if it does
proc pWriteFile {} {
    global vFileData
    if {[info exists vFileData]} {
        set id [open whatever.txt w]
        puts $id [join $vFileData \n]
        close $id
    } else {
        # handle no data
    }
    return 0
}

# call this proc with 1 argument (text to search for in vFileData and if found remove that list item)
# only the first list item matching *txt* will be removed
# this proc calls pWriteFile if a deletion was made
proc pDeleteLine {txt} {
    global vFileData
    if {[info exists vFileData]} {
        if {[set idx [lsearch $vFileData *$txt*]] != -1} {
            set vFileData [lreplace $vFileData $idx $idx]
            pWriteFile
        } else {
            # handle not found
        }
    } else {
        # handle no data
    }
    return 0
}

# call this proc with 1 argument (text to add as a list item to vFileData)
# this proc calls pWriteFile after the text is appended as a list item
proc pAddLine {txt} {
    global vFileData
    lappend vFileData $txt
    pWriteFile
    return 0
}
Workable, as long as the data isn't a monstrous size.
User avatar
TCL_no_TK
Owner
Posts: 509
Joined: Fri Aug 25, 2006 7:05 pm
Location: England, Yorkshire

Post by TCL_no_TK »

http://wiki.tcl.tk/17396 Would be a good place to start, however, if you look in the TCL FAQ part of the forum. There is a thread dedicated to File stuff :P
F
Fill
Halfop
Posts: 80
Joined: Sun Jan 18, 2009 6:08 pm

Post by Fill »

thanks guys, you were a great help!!!
Post Reply