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 

write to a text file..

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


Joined: 21 Oct 2006
Posts: 98

PostPosted: Thu Apr 19, 2007 9:07 pm    Post subject: write to a text file.. Reply with quote

when my bot add a line to a text file i need it to add a num like below.

like:
Code:

1 line
2 line
Back to top
View user's profile Send private message
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Thu Apr 19, 2007 9:12 pm    Post subject: Reply with quote

Show us your code.
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
sdays
Halfop


Joined: 21 Oct 2006
Posts: 98

PostPosted: Thu Apr 19, 2007 9:19 pm    Post subject: Reply with quote

Code:

proc addtemp {nick uhost} {
  global tempfile file
  set file [open $tempfile a]
  puts $file "$nick $uhost"
  close $file
  return 0
}
Back to top
View user's profile Send private message
sdays
Halfop


Joined: 21 Oct 2006
Posts: 98

PostPosted: Fri Apr 20, 2007 7:49 pm    Post subject: Reply with quote

i need it to count the line before adding ,like add 1 then the line_here
Back to top
View user's profile Send private message
sdays
Halfop


Joined: 21 Oct 2006
Posts: 98

PostPosted: Sat Apr 21, 2007 5:58 am    Post subject: Reply with quote

ok heres what i get now "<eggdrop> Tcl error [temp_add]: channel "file12" wasn't opened for reading


Code:

set temp "templist.txt"

proc addtemp {} {
  global file linecount temp nick
  set addtemp_file [open $temp w]
  set linecount 0
  set lines [split [read $addtemp_file] \n]
  foreach line $lines {
    incr linecount
  }
  puts $addtemp_file "$linecount $nick"
  close $addtemp_file
  return 0
}

Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sat Apr 21, 2007 7:34 am    Post subject: Reply with quote

You're opening the file in write-only mode. Check your "access" argument for your open-command. Best approach would be to first open your file as readonly, read all the data, close it, reopen it as write only (with truncation) and write the modified data.
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
sdays
Halfop


Joined: 21 Oct 2006
Posts: 98

PostPosted: Sat Apr 21, 2007 4:43 pm    Post subject: Reply with quote

i done that now other bug..

heres tcl:
Code:

proc addtemp {} {
  global linecount temp
  set addtemp_file [open $temp a+]
  set lines [split $addtemp_file "\n"]
  foreach line $lines {
  incr linecount
  }
  set linecount 0
  puts $addtemp_file "$linecount $nick"
  close $addtemp_file
  return 0
}



here's text file:
Code:

0 lomax
0 evil
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sat Apr 21, 2007 6:41 pm    Post subject: Reply with quote

It would seem you reset linecount to 0 after you've read all the lines in the file. Also, rather than using foreach to increase the count, you might considder using llength (since you already split it to a list) instead.
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Sat Apr 21, 2007 7:43 pm    Post subject: Reply with quote

+ You're not reading your file. Try
Code:
proc addtemp {} {
  global temp
  set addtemp_file [open $temp a+]
  set lines [split [read $addtemp_file] \n]
  set linecount [llength $lines]
  puts $addtemp_file "[expr {$linecount+1}] $nick"
  close $addtemp_file
  return 0
}

_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
sdays
Halfop


Joined: 21 Oct 2006
Posts: 98

PostPosted: Sat Apr 21, 2007 7:55 pm    Post subject: Reply with quote

still samething..

Code:

1 lomax
1 evil
Back to top
View user's profile Send private message
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Sat Apr 21, 2007 9:20 pm    Post subject: Reply with quote

I doubt you got any result at all because $nick does not exist. Perhaps this is better:
Code:
proc addtemp nick {
  global temp
  set addtemp_file [open $temp r+]
  set linecount [llength [split [read $addtemp_file] \n]]
  puts $addtemp_file "$linecount $nick"
  close $addtemp_file
  return 0
}

usage: addtemp <nick> (this is supposed to continue the count, so we assume that the pevious nicks are already numbered in the file).
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
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