Results 1 to 12 of 12

Thread: Reading and Editing text file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    124

    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
    RATE MY POST

  2. #2
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    124

    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.
    RATE MY POST

  4. #4
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    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.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    124

    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.
    RATE MY POST

  6. #6
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Reading and Editing text file

    Quote Originally Posted by TooLongName View Post
    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.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    124

    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
    RATE MY POST

  8. #8
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Reading and Editing text file

    Excellent - that upload really helped

    The function, I wrote, failed miserably, due to some commenting, blank lines (this is what caused unexpected termination) and encrypted strings. I have rewritten the function to sift out the 'bad' lines:

    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 DataFile.Peek <> -1
    
                If Line <> "" And Not Line.StartsWith("//") Then
    
                    '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
    
                End If
    
                'Read another line
                Line = DataFile.ReadLine
    
            End While
    
            DataFile.Close()
            Return Nothing
    
        End Function
    That should work in VS-any

    Run the sub below once to generate a more stringent text-file, called "NewProp.txt", that will be readable in Excel or OpenOffice.Calc (and by the first piece of code I wrote )

    Code:
        'Converts the propfile to a readable format
        Private Sub MakeReadablePropFile(ByVal sFileName As String)
    
            Dim DataFile As New StreamReader(sFileName)
            Dim OutputFile As New StreamWriter("NewProp.txt")
            Dim Line As String
    
            'Read the first line
            Line = DataFile.ReadLine()
    
            'Has the file been completed yet?
            While DataFile.Peek <> -1
    
                If Line <> "" And Not Line.StartsWith("//") Then OutputFile.WriteLine(Line)
    
                'Read another line
                Line = DataFile.ReadLine
    
            End While
    
            OutputFile.Close()
            DataFile.Close()
    
        End Sub
    Hope you'll get there now
    Tom

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    124

    Re: Reading and Editing text file

    Thanks alot, and one last question: how can I get this line in a DataGridView ?
    RATE MY POST

  10. #10
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    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:
    1. 'Reads data from file propItem.txt and places it in MyData
    2.     Private Function ReadDataFromFile(ByVal sFileName As String) As DataTable
    3.  
    4.         Dim DataFile As New StreamReader(sFileName)
    5.         Dim Line As String
    6.         Dim LineElements() As String
    7.         Dim i As Integer
    8.         Dim propTable As New DataTable("propItem")
    9.  
    10.         'First line is garbage and is ignored
    11.         Line = DataFile.ReadLine()
    12.  
    13.         'Second line is headers
    14.         Line = DataFile.ReadLine()
    15.  
    16.         'Retrive headers and create tables
    17.         LineElements = Line.Split(ControlChars.Tab)
    18.  
    19.         For i = 0 To LineElements.Length - 1
    20.  
    21.             propTable.Columns.Add(LineElements(i), GetType(String))
    22.  
    23.         Next
    24.  
    25.         'Read third line and continue reading
    26.         Line = DataFile.ReadLine
    27.  
    28.         While DataFile.Peek <> -1
    29.  
    30.             If Line <> "" And Not Line.StartsWith("//") Then
    31.  
    32.                 LineElements = Line.Split(ControlChars.Tab)
    33.  
    34.                 'Load read data into the table
    35.                 propTable.LoadDataRow(LineElements, True)
    36.  
    37.             End If
    38.  
    39.             Line = DataFile.ReadLine()
    40.  
    41.         End While
    42.  
    43.         'Close file and return results
    44.         DataFile.Close()
    45.  
    46.         Return propTable
    47.  
    48.     End Function

    To use the function simply write:

    vb.net Code:
    1. DataGridView1.DataSource = ReadDataFromFile("propItem.txt")

    Regards Tom

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    124

    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

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    124

    Re: Reading and Editing text file

    Uhh, Anyone ?
    RATE MY POST

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width