|
-
Feb 24th, 2008, 04:55 PM
#1
Thread Starter
New Member
[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.
-
Feb 24th, 2008, 05:00 PM
#2
Hyperactive Member
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
-
Feb 24th, 2008, 05:05 PM
#3
Thread Starter
New Member
Re: [2008] Reading from a txt file
 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
-
Feb 24th, 2008, 05:08 PM
#4
Frenzied Member
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
-
Feb 24th, 2008, 05:17 PM
#5
Hyperactive Member
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
-
Feb 24th, 2008, 05:31 PM
#6
Frenzied Member
Re: [2008] Reading from a txt file
That probably works best.
-
Feb 24th, 2008, 05:39 PM
#7
Thread Starter
New Member
Re: [2008] Reading from a txt file
 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
-
Feb 24th, 2008, 05:42 PM
#8
Frenzied Member
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
-
Feb 24th, 2008, 05:44 PM
#9
Frenzied Member
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
-
Feb 24th, 2008, 05:59 PM
#10
Thread Starter
New Member
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.
-
Feb 24th, 2008, 06:19 PM
#11
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:
Dim FILE_NAME As String = String.Empty Dim tempArray() As String Dim i As Integer = 0 If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then FILE_NAME = OpenFileDialog1.FileName Using objReader As New System.IO.StreamReader(FILE_NAME) Do While objReader.Peek() <> -1 tempArray = objReader.ReadLine.Trim.Split(" "c) 'Split using space char to get info UserAcc(i) = tempArray(0) UserPw(i) = tempArray(1) tempArray = Nothing i += 1 Loop End Using End If
-
Feb 25th, 2008, 04:26 AM
#12
Thread Starter
New Member
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:
Private Function ModCount() If Count Mod 2 = 0 Then ModCount = Count Else ModCount = Count + 1 End If Return ModCount / 2 ' divide by two because the values are seperated into two different variables End Function Public Sub LoadAccs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadAccs.Click Dim FILE_NAME As String = String.Empty Dim TempArray() As String Dim i As Integer = 1 Dim Templine() As String Dim TextLine As String If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then ' If file exists, then go on FILE_NAME = OpenFileDialog1.FileName Using objReader As New System.IO.StreamReader(FILE_NAME) ' Here it's time to count the number of lines Do While objReader.Peek() <> -1 ' To set the size of the array StatLabel.Text = ("Loading accounts...") Templine(i) = objReader.ReadLine Count = +1 i += 1 TextLine = TextLine & objReader.ReadLine() & vbNewLine Loop Dim NPuser(ModCount()) As String ' Here we make sure the arrays aren't too big, using Modcount Dim NPpw(ModCount()) As String StatLabel.Text = (Str(Count) & " Account(s) detected") i = 1 For i = 1 To ModCount() TempArray = Templine(i).Trim.Split("/"c) 'Split using space char to get info NPuser(i) = TempArray(0) ' Assign user with the first splitted values, the accounts NPpw(i) = TempArray(1) ' And then the passwords TempArray = Nothing 'Emtpy StatLabel.Text = ("Loaded account" & NPuser(i)) Next i End Using End If
Last edited by Spacey; Feb 25th, 2008 at 06:02 AM.
-
Feb 25th, 2008, 06:50 AM
#13
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:
Templine(i) = objReader.ReadLine 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:
Dim tempLine() As String = IO.File.ReadAllLines(FILE_NAME)
Then specify the bounds like this:
vb Code:
Dim NPuser(tempLine.Length - 1) As String Dim NPpw(tempLine.Length - 1) As String
and finally just loop through you tempArray
vb Code:
For i = 1 To tempArray.GetUpperbound(0)
There's no need for all the Mod crap.
-
Feb 25th, 2008, 07:51 AM
#14
Thread Starter
New Member
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:
Dim FILE_NAME As String = String.Empty Dim TempArray() As String Dim i As Integer = 1 If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then ' If file exists, then go on FILE_NAME = OpenFileDialog1.FileName End If Dim tempLine() As String = IO.File.ReadAllLines(FILE_NAME) Dim NPuser(tempLine.Length - 1) As String Dim NPpw(tempLine.Length - 1) As String For i = 1 To tempLine.GetUpperBound(0) TempArray = tempLine(i).Trim.Split("/"c) NPuser(i) = TempArray(0) NPpw(i) = TempArray(1) TempArray = Nothing Next i MsgBox(NPuser(2) & " " & NPpw(2)) 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.
-
Feb 25th, 2008, 08:54 AM
#15
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
-
Feb 25th, 2008, 09:29 AM
#16
Thread Starter
New Member
Re: [2008] Reading from a txt file
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|