Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

save textfile

Status
Not open for further replies.

stewang

Programmer
Aug 21, 2003
77
0
0
NZ
Hello all:

In my application, I'd like to add a special condition to my
file application that if we load and save a special file which is "mylibrary.txt"(just do special thing for this file), I will do something special for it, If I load or save the rest of file, they just do the things as they do in my code now.

In the "mylibray.txt", it contains some a format as following;
<songtitle>|<artist>|albumtitle>|<bitrate>|<duration second>

for example
Is This It|The Strokes|Is This It?|154462|235
Alone Together|The Strokes|Is This It?|192391|246
Last Night|The Strokes|Is This It?|197590|244

what special thing for &quot;mylibray.txt&quot; file, if I load or save it ?

When I load the mylibrary, I need to put all the songtitle in the listbox. I already implement it. which is in readtext function.

when I save them, I must conform the fotmat as above.

for example, if the listbox has some song and other files
as following;

Last Night
Alone Together
test.tk

if I click on the Last Night and save it into a text file,
then we can see a string must look like &quot;Last Night|The Strokes|Is This It?|197590|244&quot;

Could anyone help me with saving the text file, please. I have not idea how to do this.

Thanks
rgds
stewang



Code:
 package require Tk
  # the menu bar
  menu .mbar
  . configure -menu .mbar
  menu .mbar.file -tearoff 0
  .mbar add cascade -label &quot;File&quot; -underline 0 -menu .mbar.file
  .mbar.file add command -label &quot;Open&quot; -underline 0 -command openFile
  .mbar.file add command -label &quot;Save&quot; -underline 0 -command saveFile
  .mbar.file add separator
 

  # the list box
  set ::names {}
  listbox .l -listvar ::names -width 40
  pack .l -expand 1 -fill both
  # the actions
    # get some file names
    # add the new ones to the listbox
    # do something with the content of each new file
  array set ::pathes {}
  proc openFile {}   {
    # get some file names
    set filenames [tk_getOpenFile -multiple 1];   
   
        foreach fn $filenames         {
           # get simple name
           set name [file tail $fn]
           
           if { $name == &quot;mylibrary.txt&quot; }               {
                readTextFile $fn  
                return
              }  
                      
           # check if its a new one
           if {![info exists ::pathes($name)]}            {      
              
              # add the name to the array
              puts &quot;set ::pathes($name) $fn&quot;
              set ::pathes($name) $fn
              # add the name to the listbox
              lappend ::names $name
        
              # get the file content and do something with it
              set thefile [open $fn r]
              #puts &quot;the file : $thefile&quot;             
              set filecontent [read $thefile]        
              close $thefile
            }
         }
         # sort the listbox content
         set ::names [lsort $::names]
    
  }

    # get the currently selected name
    # copy the corresponding file somewhere
    # remove the name from the listbox
  proc saveFile {}   {
    # get the currently selected name
    set n [.l curselection]
    if {$n != &quot;&quot;}     {
      # get the old name
      set source [lindex $::names $n]
      # get the new name
      set target [tk_getSaveFile]
      if {$target != &quot;&quot;}       {
        # copy the file content to the new position
        file copy $::pathes($source) $target
        # remove the name from the listbox
       # removeName
      }
    }
  }
 

# read a textfile
proc  readTextFile { filenames } {
  set thefile [open &quot;mp3library.txt&quot; r]
  set line [gets $thefile]
  while {[eof $thefile] == 0} {
	puts $line

	# split the line as before
	set data [split $line &quot;|&quot;]

	# we know the name is the first element of the list
	# resulting from split
	set songnametext [lindex $data 0]

        if {![info exists ::pathes($songnametext)]}         {
            set ::pathes($songnametext) $filenames
            # add the songnametext to the listbox
            lappend ::names $songnametext

            
            # we want the rest of the list without the name
	    # lrange allows us to pick values between two indices
	    # note that end is a shortcut for [llength $data] - 1
	    set songInfor [lrange $data 1 end]
            puts &quot;songInfor :$songInfor &quot;    

	    # data associated with an array key can be a complex structure
	    set myarray($songnametext) $songInfor     
	    set line [gets $thefile]               

        }
	
         # sort the listbox content
         set ::names [lsort $::names]
    }    
}
 
Thanks you come to my question, I known the answer alredy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top