<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-gb">
	<link rel="self" type="application/atom+xml" href="https://forum.eggheads.org/app.php/feed/topic/6013" />

	<title>egghelp/eggheads community</title>
	<subtitle>Discussion of eggdrop bots, shell accounts and tcl scripts.</subtitle>
	<link href="https://forum.eggheads.org/index.php" />
	<updated>2003-11-06T05:33:54-04:00</updated>

	<author><name><![CDATA[egghelp/eggheads community]]></name></author>
	<id>https://forum.eggheads.org/app.php/feed/topic/6013</id>

		<entry>
		<author><name><![CDATA[Ofloo]]></name></author>
		<updated>2003-11-06T05:33:54-04:00</updated>

		<published>2003-11-06T05:33:54-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=29583#p29583</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=29583#p29583"/>
		<title type="html"><![CDATA[read file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=29583#p29583"><![CDATA[
omg great<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=3151">Ofloo</a> — Thu Nov 06, 2003 5:33 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[stdragon]]></name></author>
		<updated>2003-11-06T03:27:24-04:00</updated>

		<published>2003-11-06T03:27:24-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=29578#p29578</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=29578#p29578"/>
		<title type="html"><![CDATA[read file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=29578#p29578"><![CDATA[
Okay let me add some comments:<br><div class="codebox"><p>Code: </p><pre><code>proc read_last_line {fname} {  set fp [open $fname r]  # pos is the position in the file (in reverse)  # we start at 512 bytes from the end of the file  set pos 512  while {1} {    # this moves to $pos bytes before the end of the file    seek $fp -$pos end    # read to the end of the file    set data [read -nonewline $fp]    # we may have read multiple lines (if less than 512 bytes)    set lines [split $data \n]    if {[llength $lines] &gt; 1} {      # yup, we read at least 1 line and part of another, so we know      # we for sure have the complete last line, we're done      close $fp      return [lindex $lines end]    } elseif {[tell $fp] &lt; $pos} {      # Well, before I had &gt;=, but it should be &lt;.      # This happens if the entire file is &lt; 512 bytes, and we didn't read      # in multiple lines... therefore the file is a single line and we read the      # whole thing, so we're done.      close $fp      return $data    }    # Now we just repeat the process, adding another 512 bytes    incr pos 512  }}</code></pre></div>Well, the basic method is like.... imagine you have a 10 meg text file (quotes, logs, whatever). You only want the last line. Well, usually lines are less than 512 bytes. So what we do is use "seek" to skip to 512 bytes before the end of the file. It does this without reading in all the bytes between, so it's much more efficient.<br><br>So, now no matter how big the file is, we are 512 bytes from the end. So we read in the end of the file and see if it's a complete line. The way you tell if it's a complete line is to see if it has a \n inside it. E.g. a partial line plus a complete next line. The last line in the split array will be the last line in the file, and the partial ones are ignored.<br><br>If there is no partial line, then it's possible that the line is longer than 512 bytes. I mean, if you have a line that's 3k long, and we only read in the last 512 bytes, there won't be a \n in the middle right? Because the last \n is 3k from the end of the file. So, if we don't have an embedded \n, we repeat the whole process with another 512 bytes. So 512 the first time, then 1k, then 1.5k, then 2k, etc. Most lines are &lt; 512 bytes though so it probably won't ever happen.<br><br>The only other thing we have to be careful of is if the file is only one line. Then we will *never* read a partial line, because there is only one. So we check for that by seeing whether [tell $fp] is &lt; $pos. [tell] returns the file byte position. So at the end of the file (after [read]), if the byte position is less than $pos, we know we've read in the whole file. Why? Well, say the file is 300 bytes. We first did [seek $fp end-512], which basically puts us back at the beginning (you can't seek backwards past the start). Then we use [read] to read to the end. But since it's only a 300 byte file, [tell] will return 300. Since 300 &lt; 512, we know we've read the entire file. That means that if there isn't a partial line, the entire file is one line and we're done.<br><br>Well, that was probably more detail than you wanted. But there you go.<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=8">stdragon</a> — Thu Nov 06, 2003 3:27 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[Ofloo]]></name></author>
		<updated>2003-11-04T16:10:27-04:00</updated>

		<published>2003-11-04T16:10:27-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=29509#p29509</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=29509#p29509"/>
		<title type="html"><![CDATA[read file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=29509#p29509"><![CDATA[
i managed to make it work some how and am verry greatfull for what you wrote, but could you tel me what you did or xplain it little<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=3151">Ofloo</a> — Tue Nov 04, 2003 4:10 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[stdragon]]></name></author>
		<updated>2003-11-04T01:39:19-04:00</updated>

		<published>2003-11-04T01:39:19-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=29446#p29446</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=29446#p29446"/>
		<title type="html"><![CDATA[read file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=29446#p29446"><![CDATA[
I didn't test this, but I think it will work pretty well:<br><div class="codebox"><p>Code: </p><pre><code>proc read_last_line {fname} {  set fp [open $fname r]  set pos 512  while {1} {    seek $fp -$pos end    set data [read -nonewline $fp]    set lines [split $data \n]    if {[llength $lines] &gt; 1} {      close $fp      return [lindex $lines end]    } elseif {[tell $fp] &gt;= $pos} {      close $fp      return $data    }    incr pos 512  }}</code></pre></div><p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=8">stdragon</a> — Tue Nov 04, 2003 1:39 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[Ofloo]]></name></author>
		<updated>2003-11-04T01:21:10-04:00</updated>

		<published>2003-11-04T01:21:10-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=29445#p29445</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=29445#p29445"/>
		<title type="html"><![CDATA[read file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=29445#p29445"><![CDATA[
how do i read only the last line of a file ??<br><br>or beter yet how do i get the last line of a file (txt)<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=3151">Ofloo</a> — Tue Nov 04, 2003 1:21 am</p><hr />
]]></content>
	</entry>
	</feed>
