943,648 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 2243
  • VB.NET RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jun 2nd, 2009
0

Re: How to retrieve data from a .txt file

Click to Expand / Collapse  Quote originally posted by Teme64 ...
' Read the whole file to a string
FileText = My.Computer.FileSystem.ReadAllText("D:\account.txt")
I just have one comment.

The above line will read the whole file at once. if the file is big it may lead to memory problem, I prefer to read line by line, I guess my code will be faster on PII with 64mb ram.
Reputation Points: 69
Solved Threads: 19
Junior Poster
samir_ibrahim is offline Offline
155 posts
since Sep 2008
Jun 2nd, 2009
0

Re: How to retrieve data from a .txt file

Quote ...
The above line will read the whole file at once. if the file is big it may lead to memory problem, I prefer to read line by line, I guess my code will be faster on PII with 64mb ram.
If you deal with small files (a few tens or hundreds of KBs) or you need to keep the whole file in the memory anyway, using My namespace is very handy requiring only a few lines of code. Besides, My namespace does have quite a few other handy classes too, not just My.Computer.FileSystem. If you don't need to keep the whole file in the memory or the file is really large, System.IO is absolutely the better solution. What comes to the speed of the code, with modern processor there's hardly any practically significant difference.

And after all, it's a question about selecting the right "tool"
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jun 4th, 2009
0

Re: How to retrieve data from a .txt file

Click to Expand / Collapse  Quote originally posted by Teme64 ...
To get those values to text boxes goes like this
VB.NET Syntax (Toggle Plain Text)
  1. TextBox1.Text = OneLine(0)
  2. TextBox2.Text = OneLine(1)
Now TextBox1 should show "Account" and TextBox2 should show " Apple".

That explanation totally cleared things up for me, thankyou so much! now my text box is showing text from the file but its showing the whole last line. This is what i have:

VB.NET Syntax (Toggle Plain Text)
  1. Private Sub Fill()
  2.  
  3.  
  4. If My.Computer.FileSystem.FileExists("C:\Travian.txt") Then
  5.  
  6. FileText = My.Computer.FileSystem.ReadAllText("C:\Travian.txt")
  7.  
  8. FileLines = FileText.Split(CChar(Environment.NewLine))
  9. SeparatorChar = ","
  10.  
  11. For i = 0 To FileLines.GetUpperBound(0)
  12. OneLine = FileLines(i).Split(CChar(SeparatorChar))
  13.  
  14. Next i
  15. End If
  16.  
  17. txtUser.Text = OneLine(0)
  18. End Sub
After the button is clicked text shows up in txtUser but its the whole last line of my text document. Have i done something wrong here or have i given the wrong index for OneLine?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sylenas is offline Offline
14 posts
since May 2009
Jun 4th, 2009
0

Re: How to retrieve data from a .txt file

Quote ...
but its showing the whole last line.
Your last line does not have that separator character. That's why the whole line ends up to OneLine(0).

Quote ...
have i given the wrong index for OneLine?
It depends on your data, what the index should be. Start with zero and see what you get. Then try OneLine(1). You'll get an "Index out of bounds" error finally.

Can you tell me, for what purpose this
VB.NET Syntax (Toggle Plain Text)
  1. For i = 0 To FileLines.GetUpperBound(0)
  2. OneLine = FileLines(i).Split(CChar(SeparatorChar))
  3. Next i
loop is? Do you need that loop? You have a single txtUser.Text text box (showing in your code). If you know that your data should come, for example, from the first line in the file, use simply
VB.NET Syntax (Toggle Plain Text)
  1. OneLine = FileLines(0).Split(CChar(SeparatorChar))
  2. txtUser.Text = OneLine(0)
without a loop.

Or post the data you're using and explain how you should display it in the form.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jun 4th, 2009
0

Re: How to retrieve data from a .txt file

Ok so my form looks like this

Account: _______
Username: ______
Password: ______
The account, username and password are labels and the ____ represent textboxes named appropriately. When i save the data it gets put into a text document that is named like this: txtAccount.Text & ".txt"
and the data in the text file is like this:

Account: Apple
Username: Orange
Password: Banana

basically what happens now is that it is only looking for one file (but ill fix it to look for the user specified file later) and then like you said it reads the file, splits it into lines then splits each line into two parts. I want the bit after the seperator to appear in the text box basically and so far its just putting the last line of the text file in. I also changed the value of OneLine to (1) but then it said the array was out of bounds so im thinking its actually starting to read from the last line of the text file.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sylenas is offline Offline
14 posts
since May 2009
Jun 4th, 2009
0

Re: How to retrieve data from a .txt file

Ok, you have three lines which you need to show (didn't remember your first post). Your code should be something like this
VB.NET Syntax (Toggle Plain Text)
  1. ' Assuming you have multiple "triple" entries in your file
  2. For i = 0 To FileLines.GetUpperBound(0) Step 3
  3. OneLine = FileLines(i).Split(CChar(SeparatorChar))
  4. txtAccount.Text = OneLine(1).Trim
  5. OneLine = FileLines(i + 1).Split(CChar(SeparatorChar))
  6. txtUserName.Text = OneLine(1).Trim
  7. OneLine = FileLines(i + 2).Split(CChar(SeparatorChar))
  8. txtPassword.Text = OneLine(1).Trim
  9. Next i
  10.  
  11. ' Or with a single entry in the file (no loop needed)
  12. OneLine = FileLines(0).Split(CChar(SeparatorChar))
  13. txtAccount.Text = OneLine(1).Trim
  14. OneLine = FileLines(1).Split(CChar(SeparatorChar))
  15. txtUserName.Text = OneLine(1).Trim
  16. OneLine = FileLines(2).Split(CChar(SeparatorChar))
  17. txtPassword.Text = OneLine(1).Trim
Quote ...
I also changed the value of OneLine to (1) but then it said the array was out of bounds
Check that SeparatorChar variable has the correct value (it should be ":" I think, now your code is using ",").

Quote ...
im thinking its actually starting to read from the last line of the text file.
It starts from the first line. Like I commented in the code above, it all depends if you have a single

Account: Apple
Username: Orange
Password: Banana

or multiple Account, Username and Password lines. In that case, the code will loop and use the last "triple".
Last edited by Teme64; Jun 4th, 2009 at 6:01 am. Reason: Pressed Submit button by mistake
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jun 4th, 2009
0

Re: How to retrieve data from a .txt file

YES!! IT WORKED! Oh thats fantastic i'm so happy. now i gotta figure out how to get the thing to look for files =P
Thankyou so much for the help and putting up with my crap!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sylenas is offline Offline
14 posts
since May 2009
Jun 4th, 2009
0

Re: How to retrieve data from a .txt file

Hi! Nice to hear that you got answer to your problem. Could you please mark the thread as solved. Thank you!

If you run in to a problem you can't solve, when looking for the files, start a new thread for that, please.

P.s. take a look at OpenFileDialog control...
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: insert value 000011 into sql database
Next Thread in VB.NET Forum Timeline: Runing exe from UNC





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC