Results 1 to 22 of 22

Thread: Reading File into an array [RESOLVED, and then some]

  1. #1

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121

    Cool Reading File into an array [RESOLVED, and then some]

    I've opened the file successfully and put it into a textbox, but now I want to do separate lines... I've come up with this but it always makes an error...

    VB Code:
    1. opnFile.ShowDialog()
    2.         fn = opnFile.FileName ' file name
    3.         sr = File.OpenText(fn) ' my stream reader
    4.  
    5.         Dim x() As String
    6.         While sr.Peek <> -1
    7.             x(x.Length + 1) = sr.ReadLine()
    8.         End While
    9.  
    10.         txtDisplay.Lines = x
    11.         sr.Close()
    Last edited by Cagez; Mar 5th, 2003 at 02:20 PM.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    use the split function

    Split by vbcrlf
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    ... care to elaborate?

  4. #4
    Junior Member Guile.NET's Avatar
    Join Date
    Mar 2003
    Location
    www.voodoochat.com
    Posts
    31
    VB Code:
    1. opnFile.ShowDialog()
    2. Dim fn As String = opnFile.FileName
    3. Dim sr As New StreamReader(fn)
    4. Dim x As String = sr.ReadToEnd()
    5. sr.Close()
    6. textbox.text = x

    It isn't reading into an array, however, if you only plan on opening a text file, it won't matter.
    Last edited by Guile.NET; Mar 2nd, 2003 at 02:04 PM.

  5. #5

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    It needs to be an array, Guile.NET

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    sorry

    VB Code:
    1. Dim temp As String
    2. temp = txtNote.Text 'txtNote is the textbox
    3. Dim txtSplit() As String
    4. txtSplit = temp.Split(vbLf)

    if vbLf doesnt work..try vbCr or vbCrLf
    vbLf was the one that worked for me...
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7
    Junior Member Guile.NET's Avatar
    Join Date
    Mar 2003
    Location
    www.voodoochat.com
    Posts
    31
    I'm still working on this Class, but if you can use what I have, more power to you:

    VB Code:
    1. Option Compare Binary : Option Explicit On  : Option Strict On
    2.  
    3. Imports System.IO
    4.  
    5. Public Class InputStream
    6.  
    7.     Event ReadComplete(ByVal Buffer() As Byte)
    8.     Event ReadError(ByVal ErrorMessage As String)
    9.  
    10.     Private g_File As String
    11.     Private g_BufferSize As Integer
    12.  
    13.     Public Sub New(ByVal AbsFilePath As String, ByVal BufferSize As Integer)
    14.         If File.Exists(AbsFilePath) Then
    15.             g_File = AbsFilePath
    16.             g_BufferSize = BufferSize
    17.         End If
    18.     End Sub
    19.  
    20.     Public Sub BeginRead()
    21.         Dim ReadThread As New Threading.Thread(AddressOf DoRead)
    22.         ReadThread.Start()
    23.     End Sub
    24.  
    25.     Private Sub DoRead()
    26.         Dim ByteArray() As Byte, InputBuffer() As Byte
    27.         ReDim InputBuffer(g_BufferSize)
    28.         Dim Stream As BinaryReader
    29.         Try
    30.             Stream = New BinaryReader(New FileStream(g_File, FileMode.Open, FileAccess.Read, FileShare.None), System.Text.Encoding.UTF8)
    31.             Do
    32.                 InputBuffer = Stream.ReadBytes(InputBuffer.Length)
    33.                 ByteArray = AppendArray(ByteArray, InputBuffer)
    34.             Loop Until Stream.BaseStream.Position = Stream.BaseStream.Length
    35.         Catch e As Exception
    36.             RaiseEvent ReadError(e.Message)
    37.         Finally
    38.             Stream.Close()
    39.             RaiseEvent ReadComplete(ByteArray)
    40.         End Try
    41.     End Sub
    42.  
    43.     Private Function AppendArray(ByRef BaseArray() As Byte, ByRef Buffer() As Byte) As Byte()
    44.         Dim WriteIndex As Integer
    45.         Select Case IsArray(BaseArray)
    46.             Case False
    47.                 ReDim BaseArray(Buffer.Length - 1)
    48.                 WriteIndex = 0
    49.             Case True
    50.                 ReDim Preserve BaseArray(BaseArray.Length + (Buffer.Length - 1))
    51.                 WriteIndex = BaseArray.Length - Buffer.Length
    52.         End Select
    53.         Buffer.CopyTo(BaseArray, WriteIndex)
    54.         Return BaseArray
    55.     End Function
    56.  
    57. End Class

    Usage:

    VB Code:
    1. Dim WithEvents Input As InputStream
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Input = New InputStream(TheFile, 255)
    5.         Input.BeginRead()
    6.     End Sub
    7.  
    8.     Private Sub Input_ReadComplete(ByVal ByteArray() As Byte) Handles Input.ReadComplete
    9.         RTB.Text = System.Text.Encoding.UTF8.GetString(ByteArray)
    10.     End Sub

    If you can tweak it, can you post it please?

  8. #8

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    Thanks you guys.

    [LGS]Static, your code with a little tweaking works like a charm, thank you

  9. #9
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    glad I could help..im still learning vb.net its a bit different from VB6

    Just for curiosity (and for future reference) what did you tweak?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  10. #10

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    Well I had to customize it a bit for starters And, to get the lines from the file to display on different lines in the text box, I had to use txtMyBox.Lines.

    So I came out with this, which is basically your code

    VB Code:
    1. sr = File.OpenText(fn)
    2.             Dim x As String = sr.ReadToEnd
    3.             sr.Close()
    4.  
    5.             txtDisplay.Lines = Split(x, vbLf)

    Thanks again

  11. #11
    Junior Member Guile.NET's Avatar
    Join Date
    Mar 2003
    Location
    www.voodoochat.com
    Posts
    31
    I thought you said it needed to be an array?
    The arrow shot by the archer may, or may not, kill a single person. However, stratagems devised by a wise man, can kill even babes in the womb.

  12. #12

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    It is an array Guile, x gets all the text from the file them splits it into an array.

  13. #13
    Junior Member Guile.NET's Avatar
    Join Date
    Mar 2003
    Location
    www.voodoochat.com
    Posts
    31
    My Initial Code:

    VB Code:
    1. opnFile.ShowDialog()
    2. Dim fn As String = opnFile.FileName
    3. Dim sr As New StreamReader(fn)
    4. Dim x As String = sr.ReadToEnd()
    5. sr.Close()
    6. textbox.text = x

    It isn't reading into an array, however, if you only plan on opening a text file, it won't matter.
    Your Reply:

    It needs to be an array, Guile.NET
    The code you used:

    VB Code:
    1. sr = File.OpenText(fn)
    2.             Dim x As String = sr.ReadToEnd
    3.             sr.Close()
    By your own definition... that is not an array.

    All you did was load the file into a string (x), of which you split up, as if in an array - there is a big difference!

    Nonetheless, splitting it is a waste of time; the following code will do exactly the same thing:

    VB Code:
    1. txtDisplay.Text = x
    The arrow shot by the archer may, or may not, kill a single person. However, stratagems devised by a wise man, can kill even babes in the womb.

  14. #14

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    Guile, all I needed was the ned result to be an array and static helped me with that.

    As for splitting being a waste of time, no its not. If I just add the text to the text box, the file will go all on the same line, no matter whats in the file. By splitting it into lines, everything is displayed as if you were to open it in notepad.

  15. #15
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    Cagez... Guile is right. There is no need for any array or Split function


    try this:

    Dim sr As System.IO.StreamReader = System.IO.File.OpenText("c:\Path\file.txt")
    Dim txt As String = sr.ReadToEnd
    TextBox1.Text = txt
    sr.Close()

    this should work....
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  16. #16

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    That was not working before! When I did that before it kept putting everything on the same line!

    But yes, now it is working If you read the entire file into a variable, though, isn't that hard on the memory?

    (Thanks Guile for pointing that out, btw)

  17. #17
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    no..not hard at all on mem (unless your trying to load txt files that are HUGE!)

    but it is better on performance...if you have a 2000 line txt file
    and read it in line by line...that will consume cpu
    whereas if you get it all in one chomp...much quicker

    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  18. #18

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    Ahh, I see...

    Well thank you Static and thank you Guile!

    I thought the only way to get it on separate lines was the way I was doing it, I though I was doing it Guile's way, but I was actually reading line-by-line then adding it to the text box, thats why everything was on the same line.

    Thanks again

  19. #19
    Junior Member Guile.NET's Avatar
    Join Date
    Mar 2003
    Location
    www.voodoochat.com
    Posts
    31
    There is a property for the textbox that defines whether it is multi-lined, or single. Somehow, unbeknown to you, you have changed it to multi.

    For future reference, the property name is 'Multiline'.
    The arrow shot by the archer may, or may not, kill a single person. However, stratagems devised by a wise man, can kill even babes in the womb.

  20. #20
    Junior Member
    Join Date
    Feb 2002
    Posts
    31
    I could be wrong..... but wont this get rid of your varible.

    Code:
            Dim sr As System.IO.StreamReader = System.IO.File.OpenText("c:\file.txt")
            TextBox1.Text = sr.ReadToEnd
            sr.Close()

  21. #21

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    Guile, it is set to multiline purposely

  22. #22
    Junior Member Guile.NET's Avatar
    Join Date
    Mar 2003
    Location
    www.voodoochat.com
    Posts
    31
    I meant when it was only reading to one line, and then miraculously began using many.
    The arrow shot by the archer may, or may not, kill a single person. However, stratagems devised by a wise man, can kill even babes in the womb.

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