Results 1 to 10 of 10

Thread: [RESOLVED] Read randon line from txt with CFv3.5

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    24

    Resolved [RESOLVED] Read randon line from txt with CFv3.5

    Hi. I'm making a program for windows mobile 6 (uses .NET compact framework 3.5),

    The goal for the problem is to read one random line from a txt file and display it.

    I found some examples but most of all use File.ReadAllLines witch is not available in CFv3.5

    As for now, i can't do it, and i tried lots of ways.

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Read randon line from txt with CFv3.5

    Please post your Mobile questions in the Mobile forum. I've asked the mods to move this thread.

    Create a StreamReader, call ReadToEnd to get the entire contents into a String, then call Split on that String to split on the line breaks. That will give you a String array, just like File.ReadAllLines, so everything from there should be the same.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Read randon line from txt with CFv3.5

    Thread moved to '.Net \ Mobile Development' forum

    (thanks for letting us know jmcilhinney )

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    24

    Re: Read randon line from txt with CFv3.5

    Thanks jmcilhinney and thanks the mods for moving it.

    My apologies for post in the wrong section.

    I will code and if new doubts come, i will update.

    Thanks

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    24

    Re: Read randon line from txt with CFv3.5

    Hi. I have successfull coded. However in the textbox outupt it shoes a blank line before the words.

    The code:
    Code:
     Dim TextFile() As String
            Dim dir As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
            Dim FILE_NAME As String = (dir + "\\fernandopessoa.txt")
            Dim RandomNumber As Integer
            Dim RandomClass As New Random()
            Dim var As String
    
            If File.Exists(FILE_NAME) Then
                Dim objReader As New System.IO.StreamReader(FILE_NAME)
                Dim s As String = objReader.ReadToEnd
                Dim numero As String
                TextFile = s.Split(vbNewLine)
                numero = (TextFile.Length - 1)
                RandomNumber = RandomClass.Next(0, numero)
    
                var = TextFile(RandomNumber)
                Label1.Text = var
                objReader.Close()
                objReader.Dispose()
            End If
    The output
    first line of the txt is good, for example:
    "Hello world"

    However, ALL the others have first one blank line, like this:
    "
    Hello world2"

    What is wrong?

    Thanks

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Read randon line from txt with CFv3.5

    The problem is the fact that a line break is actually two characters (CR LF) and the overload of Split that you're using, which is the only one supported on the CF, will only split on single characters. I think that the best answer is to split on just CR or just LF, then loop though the resulting array and use TrimStart to trim the LF from the beginning of each element or TrimEnd to trim the CR from the end of each element.

    Also, you should turn Option Strict On, which would have caused a compilation error in your code to notify you that there was an issue, rather than compiling and running but producing incorrect results.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    24

    Re: Read randon line from txt with CFv3.5

    Hi.

    EDITED because i'm further in codding

    I have added Option Strict On and accepted suggestion about how to recode correctly.
    I also solved the blank line problem, using just vbLf.

    Here is my actual code:

    Code:
    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
    
            Dim TextFile() As String
            Dim dir As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
            Dim FILE_NAME As String = (dir + "\\fernandopessoa.txt")
            Dim RandomNumber As Integer
            Dim RandomClass As New Random()
            Dim var As String
            If File.Exists(FILE_NAME) Then
                Dim objReader As New System.IO.StreamReader(FILE_NAME)
                Dim s As String = objReader.ReadToEnd
                Dim numero As String
                TextFile = s.Split(CChar(vbLf))
                numero = CStr((TextFile.Length - 1))
                RandomNumber = RandomClass.Next(0, CInt(numero))
                var = TextFile(RandomNumber)
                Label1.Text = var
                objReader.Close()
                objReader.Dispose()
            End If
    As you can see in the code, i use random to pickup a array's content from the string and then i display it on the textbox.
    However, i dont want array's content to be shown twice. This way i don't know how to start. I have several ideas and i would like you to point me in the best direction. I thought of one idea which was to add each random numbers to a list or something and then check if existed on the list. However i have never worked with lists and as far as i know me i would make a useless loop.
    In theory (logical programing) i would program something like this:

    A: Random x (from 0, to 40)-> If x exists on List, then GoTo A: Else add x to List.
    If list content > 40 elements then msgbox "all done" - > Empty List and restart all.

    Thanks and sorry.
    Last edited by herpez; May 26th, 2010 at 07:58 AM.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    24

    Re: Read randon line from txt with CFv3.5

    By one or other way, the thread is solved.
    Thanks to all!

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Read randon line from txt with CFv3.5

    The problem with splitting on just the LF is that the CR is left behind. You can't see it but it's there. That's why I suggested trimming.

    As for unique random selections, follow the CodeBank link in my signature and check out my threads on the subject. They may need modification for the CF but you get the idea.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    24

    Re: [RESOLVED] Read randon line from txt with CFv3.5

    Quote Originally Posted by jmcilhinney View Post
    The problem with splitting on just the LF is that the CR is left behind. You can't see it but it's there. That's why I suggested trimming.

    As for unique random selections, follow the CodeBank link in my signature and check out my threads on the subject. They may need modification for the CF but you get the idea.
    Ok, thanks! I knew i didnt solve because otherwise you wouldnt say to trim. However, to my eyes it appeared to solve.
    I will check codebank and threads.

    Thanks

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