egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Writing, reading and deleting data from a file

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
Fill
Halfop


Joined: 18 Jan 2009
Posts: 80

PostPosted: Sat Feb 14, 2009 7:00 pm    Post subject: Writing, reading and deleting data from a file Reply with quote

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:

 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
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sat Feb 14, 2009 8:51 pm    Post subject: Reply with quote

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:
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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
arfer
Master


Joined: 26 Nov 2004
Posts: 436
Location: Manchester, UK

PostPosted: Sat Feb 14, 2009 8:51 pm    Post subject: Reply with quote

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:

# 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.
Back to top
View user's profile Send private message
TCL_no_TK
Owner


Joined: 25 Aug 2006
Posts: 509
Location: England, Yorkshire

PostPosted: Sat Feb 14, 2009 8:53 pm    Post subject: Reply with quote

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 Razz
_________________
TCL the misunderstood
Back to top
View user's profile Send private message Send e-mail
Fill
Halfop


Joined: 18 Jan 2009
Posts: 80

PostPosted: Wed Feb 18, 2009 4:29 pm    Post subject: Reply with quote

thanks guys, you were a great help!!!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber