Results 1 to 12 of 12

Thread: [RESOLVED] [2005] Reading Lines

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    7

    Resolved [RESOLVED] [2005] Reading Lines

    I have a code and this is how I need it to work.

    1. Download page from the internet into a string.
    2. Search through lines
    3. If a line includes a certain string, add it to a textbox
    4. Until the end of the lines.

    But mine doesnt work.

    VB Code:
    1. Private Sub btnGrab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrab.Click
    2.         Dim externalPage As String = Nothing
    3.         Dim Line As String
    4.         externalPage = WebClient1.DownloadString("http://iat.awardspace.com/text.txt")
    5.         Dim streamr As New IO.StreamReader(externalPage)
    6.         While Not streamr.EndOfStream
    7.             Line = streamr.ReadLine
    8.             If Line.Contains("S") Then
    9.                 TextBox1.Text = TextBox1.Text & Line.ToString & vbNewLine
    10.             End If
    11.         End While
    12.     End Sub

    Im fairly new to VB.NET and I know I've done something wrong. I think it's something to do with the StreamReader. Its supposed to read streams, but I dont have any. Can anybody help me get this working please?

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

    Re: [2005] Reading Lines

    Your code looks okay. Maybe try this as the Loop condition:
    Do While sr.Peek <> -1


    Loop


    Alternatively, you could use this and avoid the streamreader code:

    VB Code:
    1. Dim myStr() As String
    2.         myStr = IO.File.ReadAllLines(externalPage)
    3.         For Each str As String In myStr
    4.             If str.Contains("text") Then
    5.                 Me.TextBox1.Text = TextBox1.Text & str.ToString & vbNewLine
    6.             End If
    7.         Next
    Stim

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

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    7

    Re: [2005] Reading Lines

    Well both the StreamReader and ReadAllLines have to read files on your computer, not strings. Is there anything that could read strings?

  4. #4
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: [2005] Reading Lines

    Quote Originally Posted by IAmThomas
    Well both the StreamReader and ReadAllLines have to read files on your computer, not strings. Is there anything that could read strings?


    I don't have .Net open but something like this should work:

    VB Code:
    1. Dim externalPage() As String = Split(WebClient1.DownloadString("http://iat.awardspace.com/text.txt"), VbCrLf)
    2.  
    3.         For i As Integer = 0 To UBound(externalPage)
    4.             If externamePage(i).Contains("S") Then TextBox1.Text = TextBox1.Text & externamePage(i).ToString & vbNewLine
    5.         Next
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

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

    Re: [2005] Reading Lines

    But the string array that gets fed the text file would then be in string format would it not?
    Stim

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

  6. #6
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: [2005] Reading Lines

    Quote Originally Posted by stimbo
    But the string array that gets fed the text file would then be in string format would it not?
    Yes it would, but it's not like you couldn't convert it later if need be. Or better yet, convert it to a string to a separate variable so that you have both, and then follow the example I provided. That way, you can parse the string array as needed and you still have the stream.
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    7

    Re: [2005] Reading Lines

    Im having problems. The code isn't splitting properly. I dont think its the right character that it is being split by.

  8. #8
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: [2005] Reading Lines

    Quote Originally Posted by IAmThomas
    Im having problems. The code isn't splitting properly. I dont think its the right character that it is being split by.
    It's splitting the file by lines. If your file is multiline, then it should be splitting it just fine. If not, insert a break point somewhere or debug.print in there to see how it is breaking things up and post it here.
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    7

    Re: [2005] Reading Lines

    This is my file:
    http://iat.awardspace.com/text.txt

    This is my code:
    VB Code:
    1. Private Sub btnGrab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrab.Click
    2.  
    3.         Dim externalPage() = Split(WebClient1.DownloadString("http://iat.awardspace.com/text.txt"), vbCrLf)
    4.  
    5.         For i As Integer = 0 To UBound(externalPage)
    6.             If externalPage(i).Contains("S") Then TextBox1.Text = TextBox1.Text & externalPage(i).ToString & vbNewLine
    7.         Next
    8.  
    9.     End Sub

    And this is what happens when I execute it:

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

    Re: [2005] Reading Lines

    If you had put a messagebox after the split command you would realise that it's not splitting the file by vbCrLf.

    It should be this:

    VB Code:
    1. Dim externalPage() As String = webClient1.DownloadString("http://iat.awardspace.com/text.txt").Split(Chr(10))
    Last edited by stimbo; Jan 28th, 2007 at 02:50 PM.
    Stim

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

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    7

    Re: [2005] Reading Lines

    Thanks, I was trying to find which Chr it was but I didnt try 10


    Thanks.

  12. #12
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: [2005] Reading Lines

    Quote Originally Posted by IAmThomas
    Thanks, I was trying to find which Chr it was but I didnt try 10


    Thanks.
    You should always know what you're working with. I assumed that each line was split by a standard carriage return, because 9 times out of 10 they are. In any case, the code should work fine now if you replace the VbCrLf with Chr$(10)
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

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