Hello.
How can I read last line of file that is being changed every 300ms?
This is log from game. I need to read every line that is added to this file.
Game saves this file every time the new line appears.
This file is preety big. So how can I read last line from this file when new version is saved by game?
I don't want to read whole file. Only last line.
Pseudo Code:
OnFileChange
{
string line = ReadLastLine();
...do sth with this line
}
5 replies to this topic
#1
Posted 19 September 2010 - 04:13 PM
|
|
|
#2
Posted 20 September 2010 - 12:31 AM
well i just did a little bit of google search and this is what i hit with
and i want you to have a good look at the Seek method of the File Stream class over here
Dim nl As String = System.Environment.NewLine
Dim nllen As Integer = nl.Length
Dim s1 As String = "This is " + nl + "a multiline " + nl + "and very long sentence" + nl
Dim s2 As String
If s1.LastIndexOf(nl) = s1.Length - nllen Then
s2 = s1.Substring(0, s1.Length - nllen).Substring(s1.Substring(0, s1.Length - nllen).LastIndexOf(nl) + nllen)
Else
s2 = s1.Substring(s1.LastIndexOf(nl) + 2)
End If
MessageBox.Show("(1)" + s1 + nl + "(2)" + s2 + "(3)END MARKER")
and i want you to have a good look at the Seek method of the File Stream class over here
http://msdn.microsoft.com/en-us/library/system.io.filestream_members.aspx
#3
Posted 20 September 2010 - 04:56 AM
I would solve this problem in two steps:
1. find beginning of the last line at application startup
2. iteratively read newly added lines at regular work
First step may look difficult. For example, if your file is 10MB long, there's no point in reading 10MB just to find that last line occupies final 100 bytes of it. So you start with presumption that last line is, say, 1 byte long (but probably better assumption is 100 bytes or so). Then seek to file length minus assumed length and read to the end of the file. If you find end of line as gokuajmes specified, then you're done. Otherwise, double the assumed line length and try again. Finally, end of line should appear and then you have captured the last line.
After finding the last line at application startup, you can just seek and read rest of the file in regular intervals. Do not just read the file because you'll always hit EOF and further reading will fail. Rather seek to previous position and then read every time - that will enforce reading after previous EOF once EOF moves after logger writes to the file.
1. find beginning of the last line at application startup
2. iteratively read newly added lines at regular work
First step may look difficult. For example, if your file is 10MB long, there's no point in reading 10MB just to find that last line occupies final 100 bytes of it. So you start with presumption that last line is, say, 1 byte long (but probably better assumption is 100 bytes or so). Then seek to file length minus assumed length and read to the end of the file. If you find end of line as gokuajmes specified, then you're done. Otherwise, double the assumed line length and try again. Finally, end of line should appear and then you have captured the last line.
After finding the last line at application startup, you can just seek and read rest of the file in regular intervals. Do not just read the file because you'll always hit EOF and further reading will fail. Rather seek to previous position and then read every time - that will enforce reading after previous EOF once EOF moves after logger writes to the file.
#4
Posted 20 September 2010 - 09:28 AM
zoranh said:
After finding the last line at application startup, you can just seek and read rest of the file in regular intervals. Do not just read the file because you'll always hit EOF and further reading will fail. Rather seek to previous position and then read every time - that will enforce reading after previous EOF once EOF moves after logger writes to the file.
But there is one problem.
Log file can't be save when i'ts open in my c# application.
Pastebin.com
#5
Posted 20 September 2010 - 09:37 AM
When creating the FileStream object you should use FileShare.ReadWrite as the last parameter. This allows the logger to write to the file while you have it open.
#6
Posted 20 September 2010 - 09:50 AM
dbug said:
When creating the FileStream object you should use FileShare.ReadWrite as the last parameter. This allows the logger to write to the file while you have it open.
But.
When I read last line. Than file updates(new line apperars). And Than I read the line I get empt line. And than again read the line, I get proper line. So, after change I have to read the line 2 times to get last line.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









