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.

I want to edit a line but clobbers the whole file

Help for those learning Tcl or writing their own scripts.
Post Reply
a
admiralboom
Voice
Posts: 3
Joined: Fri Oct 30, 2009 11:54 am

I want to edit a line but clobbers the whole file

Post by admiralboom »

I need a suggestion on appending a line in the file without clobbering the whold file, suggestions?

Code: Select all

bind pub - !status proc_status
proc proc_status { ircnick userhost handle channel arg } {
  set status "$arg"
  if {$status == ""} {
     puthelp "PRIVMSG $ircnick :Please supply your status"
     return 0
  }
   set fs [open "/srv/httpd/htdocs/status.txt" r]
   set tmpfs [open "/srv/httpd/htdocs/tmpstatus.txt" w]
   set change 0
   while {![eof $fs]} {
        if {![gets $fs line]} {
          puts -nonewline $tmpfs "$line\n"
        } elseif {[lindex $line 1] == $ircnick} {
          puts -nonewline $tmpfs "[unixtime] $ircnick $status\n"
          set change 1
        }
    }
   if {!$change} {
        puts -nonewline $tmpfs "[unixtime] $ircnick $status"
           puthelp "PRIVMSG $ircnick : Your status has been added."
        }  else {
           puthelp "PRIVMSG $ircnick : Your status has been updated."
        }
   close $fs
   close $tmpfs
   set tmpfs [open "/srv/httpd/htdocs/tmpstatus.txt" r]
   set statusdb "[read $tmpfs]"
   close $tmpfs
   set fs [open "/srv/httpd/htdocs/status.txt" w]
   puts -nonewline $fs "$statusdb"
   close $fs
   return 0
}
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

if you want to append some data to end of file, use just

Code: Select all

set some_file_to_append [open $file a]
http://www.tcl.tk/man/tcl8.4/TclCmd/open.htm#M9
a
admiralboom
Voice
Posts: 3
Joined: Fri Oct 30, 2009 11:54 am

Post by admiralboom »

I suppose so although how do I tell it which line to edit?
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

ahhh, my fault...

you can read whole file and later you can use some loop (for, foreach) to check the line

it will be smth like:
1. open file 'r', read whole content to some Var, close the file
2. open the same filem with 'w'
3. copy all from Var to this file (insert some IF inside loop, if line match - write it, if not - don't)
3. close the file

file.txt:

Code: Select all

one
two
three
example.tcl:

Code: Select all

#!/usr/bin/tclsh


set read_data [open "file.txt" r]
set all_lines [split [read $read_data] "\n"]
close $read_data

set change "two"

set write_data [open "file.txt" w]
foreach file_line $all_lines {
        if {$file_line != ""} {
                if {$file_line == $change} {
                        puts $write_data "woohooo"
                } {
                        puts $write_data $file_line
                }
        }
}
close $write_data
file.txt after:

Code: Select all

one
woohoo
three
Its possible to this with one 'file open' by using 'seek' and 'a+' mode.
Last edited by tomekk on Sat Oct 31, 2009 2:26 pm, edited 1 time in total.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

This should be helpful: Basic File Operations
a
admiralboom
Voice
Posts: 3
Joined: Fri Oct 30, 2009 11:54 am

Post by admiralboom »

Thank you tomekk that did it.
Post Reply