How to retrieve data from a .txt file

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 148
Reputation: samir_ibrahim is on a distinguished road 
Solved Threads: 16
samir_ibrahim's Avatar
samir_ibrahim samir_ibrahim is offline Offline
Junior Poster

Re: How to retrieve data from a .txt file

 
0
  #11
Jun 2nd, 2009
Originally Posted by Teme64 View Post
' 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 115
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: How to retrieve data from a .txt file

 
0
  #12
Jun 2nd, 2009
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"
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 14
Reputation: Sylenas is an unknown quantity at this point 
Solved Threads: 0
Sylenas Sylenas is offline Offline
Newbie Poster

Re: How to retrieve data from a .txt file

 
0
  #13
Jun 4th, 2009
Originally Posted by Teme64 View Post
To get those values to text boxes goes like this
  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:

  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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 115
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: How to retrieve data from a .txt file

 
0
  #14
Jun 4th, 2009
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).

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
  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
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 14
Reputation: Sylenas is an unknown quantity at this point 
Solved Threads: 0
Sylenas Sylenas is offline Offline
Newbie Poster

Re: How to retrieve data from a .txt file

 
0
  #15
Jun 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 115
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: How to retrieve data from a .txt file

 
0
  #16
Jun 4th, 2009
Ok, you have three lines which you need to show (didn't remember your first post). Your code should be something like this
  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
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 ",").

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
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 14
Reputation: Sylenas is an unknown quantity at this point 
Solved Threads: 0
Sylenas Sylenas is offline Offline
Newbie Poster

Re: How to retrieve data from a .txt file

 
0
  #17
Jun 4th, 2009
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!!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 115
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: How to retrieve data from a .txt file

 
0
  #18
Jun 4th, 2009
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...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1141 | Replies: 17
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC