|
-
Jul 23rd, 2010, 01:28 PM
#1
Thread Starter
Lively Member
Reading and Editing text file
Hello,
I have a text file called: list.txt
This is a list with all kind of lines like this:
Code:
14 II_ARM_S_CLO_CLO_CLOVER IDS_PROPITEM_TXT_011946 1 1 IK1_ARMOR IK2_CLOTH IK3_CLOAK = TRUE = = 2000 50000 = = = = PARTS_CLOAK = 1 = = 1 1 = 1 = = = 1 1 _NONE
Ofcourse every line has different numbers and letters.
The complicated bit is this:
I want to let Button1 check whats in TextBox1.Text and search "list.txt" for it. When it has got a match (there is only ONE of these numbers in list.txt).
For example TextBox1.Text = "IDS_PROPITEM_TXT_011946". And I search for that so I find the line above. Now I want 3 other textboxes which show the 21st, 22nd, 23rd column/number after "IDS_PROPITEM_TXT_011946". And they should be editable by changing those 3 textboxes and clicking the apply button.
How can I best do this, and please give a little example.
~Thanks in Advance
-
Jul 23rd, 2010, 01:55 PM
#2
Re: Reading and Editing text file
Searching the file for any matching symbol is highly inefficient in most cases. If you need to extract loads of information from a relatively small file, you'll probably be better off loading all the information into a DataSet or DataTable and then saving it back to the file upon completion.
Reading a standard [tab] seperated text file is easily done. For instance this function searches the file for a specific symbol and returns the line in which it's found:
Code:
'Will find and return the line in the specified file containing sSymbol (or Nothing if none exists)
Private Function FindSymbolInFile(ByVal sFileName As String, ByVal sSymbol As String) As String
Dim DataFile As New StreamReader(sFileName)
Dim Line As String
Dim LineElements() As String
Dim i As Integer
'Read the first line
Line = DataFile.ReadLine()
'Has the file been completed yet?
While Line <> ""
'Split the line into bits of information each tab position.
LineElements = Line.Split(ControlChars.Tab)
For i = 0 To LineElements.Length - 1
'If the symbol is found, return the line
If LineElements(i) = sSymbol Then
DataFile.Close()
Return Line
End If
Next
'Read another line
Line = DataFile.ReadLine
End While
DataFile.Close()
Return Nothing
End Function
Supposing the IDS_PROPITEM allways occur at position 3 in the file. A much more efficient way of searching for that particular symbol would then be:
Code:
Private Function FindPropSymbol(ByVal sFileName As String, ByVal sSymbol As String) As String
Dim DataFile As New StreamReader(sFileName)
Dim Line As String
Dim LineElements() As String
'Read the first line
Line = DataFile.ReadLine()
'Has the file been completed yet?
While Line <> ""
'Split the line into bits of information each tab position.
LineElements = Line.Split(ControlChars.Tab)
If LineElements.Length > 2 Then
'If the prop-symbol is found, return the line
If LineElements(2) = sSymbol Then '2 because split returns 0-based arrays
DataFile.Close()
Return Line
End If
End If
'Read another line
Line = DataFile.ReadLine
End While
DataFile.Close()
Return Nothing
End Function
Hope this helps a bit.
Regards Tom
Last edited by ThomasJohnsen; Jul 23rd, 2010 at 01:58 PM.
-
Jul 24th, 2010, 05:11 AM
#3
Thread Starter
Lively Member
Re: Reading and Editing text file
Okay,
I'm using this code now:
Code:
Private Function FindSymbolInFile(ByVal sFileName As String, ByVal sSymbol As String) As String
Dim DataFile As New StreamReader(sFileName)
Dim Line As String
Dim LineElements() As String
Dim i As Integer
'Read the first line
Line = DataFile.ReadLine()
'Has the file been completed yet?
While Line <> ""
'Split the line into bits of information each tab position.
LineElements = Line.Split(ControlChars.Tab)
For i = 0 To LineElements.Length - 1
'If the symbol is found, return the line
If LineElements(i) = sSymbol Then
DataFile.Close()
TextBox3.Text = Line
End If
Next
'Read another line
Line = DataFile.ReadLine
End While
DataFile.Close()
TextBox3.Text = "Nothing Found"
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
FindSymbolInFile(Application.StartupPath & "\propItem.txt", TextBox2.Text)
End Sub
And it always shows "Nothing Found" in TextBox3
This "IDS_PROPITEM_TXT_011454" is in propItem.txt, I can search for it by Notepad easily.
But the code never shows the whole line in which "IDS_PROPITEM_TXT_011454" is.
It would be nice if it WOULD read the line and put it into a DataGridView or something like that.
-
Jul 24th, 2010, 06:46 AM
#4
Re: Reading and Editing text file
Before you changed it, this was a function. When a function returns a value, it terminates automatically, while your changes makes it complete the search even after a match is found. Try adding the bolded line:
Code:
'If the symbol is found, return the line
If LineElements(i) = sSymbol Then
DataFile.Close()
TextBox3.Text = Line
return "" 'Should be Exit Sub since you've changed it to a Sub basically.
End If
#EDIT: A better solution would be to change it back to what it was (replace the line [Return nothing] with [Return "No match found"] and changing your button2 click event to:
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox3.Text = FindSymbolInFile(Application.StartupPath & "\propItem.txt", TextBox2.Text)
End Sub
Last edited by ThomasJohnsen; Jul 24th, 2010 at 06:53 AM.
-
Jul 24th, 2010, 11:58 AM
#5
Thread Starter
Lively Member
Re: Reading and Editing text file
I did exactly as you said, this is my code now:
Code:
Private Function FindSymbolInFile(ByVal sFileName As String, ByVal sSymbol As String) As String
Dim DataFile As New StreamReader(sFileName)
Dim Line As String
Dim LineElements() As String
Dim i As Integer
'Read the first line
Line = DataFile.ReadLine()
'Has the file been completed yet?
While Line <> ""
'Split the line into bits of information each tab position.
LineElements = Line.Split(ControlChars.Tab)
For i = 0 To LineElements.Length - 1
'If the symbol is found, return the line
If LineElements(i) = sSymbol Then
DataFile.Close()
Return Line
End If
Next
'Read another line
Line = DataFile.ReadLine
End While
DataFile.Close()
Return "Found Nothing"
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox3.Text = FindSymbolInFile(Application.StartupPath & "\propItem.txt", TextBox2.Text)
End Sub
And when I search for "IDS_PROPITEM_TXT_011454", which IS in propItem.txt, it simply returns "Found Nothing".
I'm clueless.
-
Jul 24th, 2010, 02:21 PM
#6
Re: Reading and Editing text file
 Originally Posted by TooLongName
And when I search for "IDS_PROPITEM_TXT_011454", which IS in propItem.txt, it simply returns "Found Nothing".
I'm clueless.
I have tried it several times on my own tab seperated files and it works here.
The file is probably not tab-seperated then (though it looks that way) or maybe you are using some sort of encoding on the file that causes input to be read faulty. Try to output the LineElements to console to verify, that encoding and tab-seperation is valid:
Code:
...
For i = 0 To LineElements.Length - 1
Console.Write(LineElements(i) & ", ")
'If the symbol is found, return the line
If LineElements(i) = sSymbol Then
DataFile.Close()
Return Line
End If
Next
Console.WriteLine()
...
#EDIT: Oh and please supply part of the output, so it's easier to understand, whats going on.
#EDIT2: Just tested it in VS2010 - works there too (yeah I downloaded the free 30 days trial and it took forever to download and install ).
Last edited by ThomasJohnsen; Jul 24th, 2010 at 02:45 PM.
-
Jul 25th, 2010, 04:19 AM
#7
Thread Starter
Lively Member
Re: Reading and Editing text file
Still doesn't work. I uploaded my propItem.txt so you can try it easier:
http://www.mediafire.com/?edkge2ipifkazwt
#EDIT: Oh and please supply part of the output, so it's easier to understand, whats going on.
#EDIT2: Just tested it in VS2010 - works there too (yeah I downloaded the free 30 days trial and it took forever to download and install ).
I'm so sorry for you that I don't have VS2010 
I have VS2008
-
Jul 25th, 2010, 05:02 AM
#8
Re: Reading and Editing text file
-
Jul 25th, 2010, 03:23 PM
#9
Thread Starter
Lively Member
Re: Reading and Editing text file
Thanks alot, and one last question: how can I get this line in a DataGridView ?
-
Jul 25th, 2010, 04:45 PM
#10
Re: Reading and Editing text file
Should be fairly straightforward - but be warned: the data is only formatted as strings and not processed in any way. The function below will read the data into a datatable called "propItem" and return it.
vb.net Code:
'Reads data from file propItem.txt and places it in MyData Private Function ReadDataFromFile(ByVal sFileName As String) As DataTable Dim DataFile As New StreamReader(sFileName) Dim Line As String Dim LineElements() As String Dim i As Integer Dim propTable As New DataTable("propItem") 'First line is garbage and is ignored Line = DataFile.ReadLine() 'Second line is headers Line = DataFile.ReadLine() 'Retrive headers and create tables LineElements = Line.Split(ControlChars.Tab) For i = 0 To LineElements.Length - 1 propTable.Columns.Add(LineElements(i), GetType(String)) Next 'Read third line and continue reading Line = DataFile.ReadLine While DataFile.Peek <> -1 If Line <> "" And Not Line.StartsWith("//") Then LineElements = Line.Split(ControlChars.Tab) 'Load read data into the table propTable.LoadDataRow(LineElements, True) End If Line = DataFile.ReadLine() End While 'Close file and return results DataFile.Close() Return propTable End Function
To use the function simply write:
vb.net Code:
DataGridView1.DataSource = ReadDataFromFile("propItem.txt")
Regards Tom
-
Jul 31st, 2010, 03:37 AM
#11
Thread Starter
Lively Member
Re: Reading and Editing text file
Ok, its working fine, everything is loaded into the DataGridView1 perfectly.
One more question: How can I save the changes 
Thanks in Advance
Last edited by TooLongName; Jul 31st, 2010 at 06:57 AM.
RATE MY POST
-
Aug 7th, 2010, 08:08 AM
#12
Thread Starter
Lively Member
Re: Reading and Editing text file
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|