Results 1 to 16 of 16

Thread: [RESOLVED] [2008] Reading from a txt file

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    12

    Resolved [RESOLVED] [2008] Reading from a txt file

    Hello everyone, I'm writing a little application that is supposed to fetch account information from a txt file, the format is going to be

    account pw
    account pw
    account pw

    And this information should be stored into 2 different arrays, UserAcc(string) and UserPw(string). I have already written the script for determining the size of the arrays.

    But how would I go about reading and storing the account and password information into the right variables?
    Last edited by Spacey; Feb 25th, 2008 at 06:03 AM.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    413

    Re: [2008] Reading from a txt file

    Homework? I'd be looking at the streamreader and streamwriter classes.
    Visual Studio .NET 2005/.NET Framework 2.0

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    12

    Re: [2008] Reading from a txt file

    Quote Originally Posted by PENNYSTOCK
    Homework? I'd be looking at the streamreader and streamwriter classes.
    I've done that, but the thing is I know how to set the size of the arrays counting the amount of lines with streamreader, but I don't know how to go about dividing the lines into two different variables based on acc/pw

  4. #4
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: [2008] Reading from a txt file

    You could fill up a listbox and try something like this:

    Code:
    Dim i as integer = 0 to list.items.count-1
    UserAcc (i) = leftof (list.items.item(i), " ")
    userpw(i) = rightof (lst.items.item(i),useracc(i) & " ")
    
    Public Function LeftOf(ByVal SearchIn As String, ByVal StartLeft As String) As String
            LeftOf = strings.Replace(SearchIn, strings.Right(SearchIn, Len(SearchIn) - Val(InStr(1, SearchIn, StartLeft)) + 1), vbnullstring)
    End Function
    
    Public Function RightOf(ByVal SearchIn as string, Byval startleft as string)as string
            RightOf = strings.Right(SearchIn, Len(SearchIn) - (Val(InStr(1, SearchIn, StartRight)) + Len(StartRight)))
    end function

  5. #5
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    413

    Re: [2008] Reading from a txt file

    I'd use the readline into a string, and use string.split(" ") to get the two halves.
    Visual Studio .NET 2005/.NET Framework 2.0

  6. #6
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: [2008] Reading from a txt file

    That probably works best.

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    12

    Re: [2008] Reading from a txt file

    Quote Originally Posted by PENNYSTOCK
    I'd use the readline into a string, and use string.split(" ") to get the two halves.
    What exactly does String.split do though? I need to make sure that they are placed in their respective variables

  8. #8
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: [2008] Reading from a txt file

    Code:
    Dim theArray() As String
        theArray = String.Split("user pass", " ")
        For i As Integer= LBound(theArray) To UBound(theArray)
        Debug.Print theArray(i)
        Next i

  9. #9
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: [RESOLVED] [2008] Reading from a txt file

    Code:
    Private Sub Command1_Click()
    Dim theArray() As String
        theArray = String.Split("user pass", " ")
        For i As Integer= LBound(theArray) To UBound(theArray)
            If IsOdd(i) = False Then Debug.Print theArray(i) & " - user"
            If IsOdd(i) = True Then Debug.Print theArray(i) & " - pass"
        Next i
    End Sub
    
    Function IsOdd(ByVal lngTheNumber As Long) As Boolean
        IsOdd = IIf((lngTheNumber Mod 2) = 0, False, True)
    End Function

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    12

    Re: [2008] Reading from a txt file

    Actually I'm getting error if I try to use string split..

    Here's my current code
    Code:
        Private Sub LoadAccs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadAccs.Click
    
            OpenFileDialog1.ShowDialog()
            Dim FILE_NAME As String
            FILE_NAME = OpenFileDialog1.FileName
    
            Dim TextLine As String
            Dim objReader As New System.IO.StreamReader(FILE_NAME)
            Dim i As Integer
            Dim splitter As String
    
            If System.IO.File.Exists(OpenFileDialog1.FileName) = True Then
    
                Do While objReader.Peek() <> -1
                    TextLine = objReader.ReadLine() & vbNewLine
                    splitter = objReader.ReadLine().ToString()
                    splitter.Split("user pw", " ")
                    Count = +1
                    i = +1
                Loop
    
            Else
    
                MsgBox("File Does Not Exist")
    
            End If
    
            objReader.Close()
    
    
        End Sub
    Last edited by Spacey; Feb 24th, 2008 at 06:02 PM.

  11. #11
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2008] Reading from a txt file

    if you have already correctly specified the size of the arrays for your password and user account info then this should work (it's based on the assumption that there's a space between the 2 pieces of info so that's the character that you will be splitting by):

    vb Code:
    1. Dim FILE_NAME As String = String.Empty
    2. Dim tempArray() As String
    3. Dim i As Integer = 0
    4.  
    5.         If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    6.             FILE_NAME = OpenFileDialog1.FileName
    7.  
    8.             Using objReader As New System.IO.StreamReader(FILE_NAME)
    9.                 Do While objReader.Peek() <> -1
    10.                     tempArray = objReader.ReadLine.Trim.Split(" "c)  'Split using space char to get info
    11.                     UserAcc(i) = tempArray(0)
    12.                     UserPw(i) = tempArray(1)
    13.                     tempArray = Nothing
    14.                     i += 1
    15.                 Loop
    16.             End Using
    17.         End If
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  12. #12

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    12

    Re: [RESOLVED] [2008] Reading from a txt file

    Well thanks for the help, here is the finished code. But I'm getting errors when I try to load the accounts, any ideas what might be wrong?

    vb Code:
    1. Private Function ModCount()
    2.  
    3.         If Count Mod 2 = 0 Then
    4.             ModCount = Count
    5.         Else
    6.             ModCount = Count + 1
    7.         End If
    8.  
    9.         Return ModCount / 2 ' divide by two because the values are seperated into two different variables
    10.  
    11.     End Function
    12.  
    13.     Public Sub LoadAccs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadAccs.Click
    14.  
    15.         Dim FILE_NAME As String = String.Empty
    16.         Dim TempArray() As String
    17.         Dim i As Integer = 1
    18.         Dim Templine() As String
    19.         Dim TextLine As String
    20.  
    21.         If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then ' If file exists, then go on
    22.             FILE_NAME = OpenFileDialog1.FileName
    23.  
    24.             Using objReader As New System.IO.StreamReader(FILE_NAME) ' Here it's time to count the number of lines
    25.                 Do While objReader.Peek() <> -1 ' To set the size of the array
    26.                     StatLabel.Text = ("Loading accounts...")
    27.                     Templine(i) = objReader.ReadLine
    28.                     Count = +1
    29.                     i += 1
    30.                     TextLine = TextLine & objReader.ReadLine() & vbNewLine
    31.                 Loop
    32.  
    33.                 Dim NPuser(ModCount()) As String ' Here we make sure the arrays aren't too big, using Modcount
    34.                 Dim NPpw(ModCount()) As String
    35.                 StatLabel.Text = (Str(Count) & " Account(s) detected")
    36.                 i = 1
    37.  
    38.                 For i = 1 To ModCount()
    39.                     TempArray = Templine(i).Trim.Split("/"c)  'Split using space char to get info
    40.                     NPuser(i) = TempArray(0) ' Assign user with the first splitted values, the accounts
    41.                     NPpw(i) = TempArray(1) ' And then the passwords
    42.                     TempArray = Nothing 'Emtpy
    43.                     StatLabel.Text = ("Loaded account" & NPuser(i))
    44.                 Next i
    45.             End Using
    46.  
    47.         End If
    Last edited by Spacey; Feb 25th, 2008 at 06:02 AM.

  13. #13
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2008] Reading from a txt file

    But I'm getting errors when I try to load the accounts, any ideas what might be wrong?
    How about you actually tell us what the errors are and then we might be able to tell you what the problem is...

    Although i'm guessing one of the reasons is because you aren't specifying the bounds of the arrays and then trying to add elements to the array.

    In your first use of the StreamReader you have these two lines. Each time you use .ReadLine it will do just that
    vb Code:
    1. Templine(i) = objReader.ReadLine
    2.  
    3. TextLine = TextLine & objReader.ReadLine() & vbNewLine
    So for each 'loop' you read two lines. Are you sure this is what you want to do?

    I have no idea what the point of TextLine is.

    You would really be better off just writing this to fill your array:
    vb Code:
    1. Dim tempLine() As String = IO.File.ReadAllLines(FILE_NAME)

    Then specify the bounds like this:

    vb Code:
    1. Dim NPuser(tempLine.Length - 1) As String
    2. Dim NPpw(tempLine.Length - 1) As String

    and finally just loop through you tempArray

    vb Code:
    1. For i = 1 To tempArray.GetUpperbound(0)

    There's no need for all the Mod crap.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  14. #14

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    12

    Re: [2008] Reading from a txt file

    The error is unhandled expception, object reference not set to an instance of an object.

    Okay, I'm fairly close now after experimenting awhile, everything runs fine, but out of some weird reason it fetches one line too much. For example if I want to print out NPpw(2) it would print out test3 (from the third line)

    Here's my code

    vb Code:
    1. Dim FILE_NAME As String = String.Empty
    2.         Dim TempArray() As String
    3.         Dim i As Integer = 1
    4.  
    5.         If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then ' If file exists, then go on
    6.             FILE_NAME = OpenFileDialog1.FileName
    7.         End If
    8.  
    9.         Dim tempLine() As String = IO.File.ReadAllLines(FILE_NAME)
    10.         Dim NPuser(tempLine.Length - 1) As String
    11.         Dim NPpw(tempLine.Length - 1) As String
    12.  
    13.         For i = 1 To tempLine.GetUpperBound(0)
    14.             TempArray = tempLine(i).Trim.Split("/"c)
    15.             NPuser(i) = TempArray(0)
    16.             NPpw(i) = TempArray(1)
    17.             TempArray = Nothing
    18.         Next i
    19.  
    20.         MsgBox(NPuser(2) & " " & NPpw(2))
    21.  
    22.  
    23.     End Sub

    Okay I kind of realized the reason for this happening, and it's because the first line is stored in (0), but yeah.. is there any way to fix this? So that the lines are stored in the correct order
    Last edited by Spacey; Feb 25th, 2008 at 08:28 AM.

  15. #15
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2008] Reading from a txt file

    The lines are in the correct order and as you said is because arrays are zero based.

    Why can't you just use it as it is, it's perfectly fine.

    Otherwise you will have to create an array with an extra space and then always have a completely empty element in your array which to me is just unnecessary.

    Setting i to 1 in the loop will mean you will obviously lose the 1st line of your text file as it will be in the 0 position in the array
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  16. #16

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    12

    Re: [2008] Reading from a txt file

    Quote Originally Posted by stimbo
    The lines are in the correct order and as you said is because arrays are zero based.

    Why can't you just use it as it is, it's perfectly fine.

    Otherwise you will have to create an array with an extra space and then always have a completely empty element in your array which to me is just unnecessary.

    Setting i to 1 in the loop will mean you will obviously lose the 1st line of your text file as it will be in the 0 position in the array
    Yeah I actually fixed that, I just didn't bother updating the code here. Anyhow I'll mark this as resolved now thanks for all the help

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