|
-
Mar 1st, 2003, 10:45 PM
#1
Thread Starter
Lively Member
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:
opnFile.ShowDialog()
fn = opnFile.FileName ' file name
sr = File.OpenText(fn) ' my stream reader
Dim x() As String
While sr.Peek <> -1
x(x.Length + 1) = sr.ReadLine()
End While
txtDisplay.Lines = x
sr.Close()
Last edited by Cagez; Mar 5th, 2003 at 02:20 PM.
-
Mar 1st, 2003, 11:45 PM
#2
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"
-
Mar 2nd, 2003, 01:38 PM
#3
Thread Starter
Lively Member
-
Mar 2nd, 2003, 01:59 PM
#4
Junior Member
VB Code:
opnFile.ShowDialog()
Dim fn As String = opnFile.FileName
Dim sr As New StreamReader(fn)
Dim x As String = sr.ReadToEnd()
sr.Close()
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.
-
Mar 2nd, 2003, 02:03 PM
#5
Thread Starter
Lively Member
It needs to be an array, Guile.NET
-
Mar 2nd, 2003, 02:04 PM
#6
sorry
VB Code:
Dim temp As String
temp = txtNote.Text 'txtNote is the textbox
Dim txtSplit() As String
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"
-
Mar 2nd, 2003, 02:11 PM
#7
Junior Member
I'm still working on this Class, but if you can use what I have, more power to you:
VB Code:
Option Compare Binary : Option Explicit On : Option Strict On
Imports System.IO
Public Class InputStream
Event ReadComplete(ByVal Buffer() As Byte)
Event ReadError(ByVal ErrorMessage As String)
Private g_File As String
Private g_BufferSize As Integer
Public Sub New(ByVal AbsFilePath As String, ByVal BufferSize As Integer)
If File.Exists(AbsFilePath) Then
g_File = AbsFilePath
g_BufferSize = BufferSize
End If
End Sub
Public Sub BeginRead()
Dim ReadThread As New Threading.Thread(AddressOf DoRead)
ReadThread.Start()
End Sub
Private Sub DoRead()
Dim ByteArray() As Byte, InputBuffer() As Byte
ReDim InputBuffer(g_BufferSize)
Dim Stream As BinaryReader
Try
Stream = New BinaryReader(New FileStream(g_File, FileMode.Open, FileAccess.Read, FileShare.None), System.Text.Encoding.UTF8)
Do
InputBuffer = Stream.ReadBytes(InputBuffer.Length)
ByteArray = AppendArray(ByteArray, InputBuffer)
Loop Until Stream.BaseStream.Position = Stream.BaseStream.Length
Catch e As Exception
RaiseEvent ReadError(e.Message)
Finally
Stream.Close()
RaiseEvent ReadComplete(ByteArray)
End Try
End Sub
Private Function AppendArray(ByRef BaseArray() As Byte, ByRef Buffer() As Byte) As Byte()
Dim WriteIndex As Integer
Select Case IsArray(BaseArray)
Case False
ReDim BaseArray(Buffer.Length - 1)
WriteIndex = 0
Case True
ReDim Preserve BaseArray(BaseArray.Length + (Buffer.Length - 1))
WriteIndex = BaseArray.Length - Buffer.Length
End Select
Buffer.CopyTo(BaseArray, WriteIndex)
Return BaseArray
End Function
End Class
Usage:
VB Code:
Dim WithEvents Input As InputStream
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Input = New InputStream(TheFile, 255)
Input.BeginRead()
End Sub
Private Sub Input_ReadComplete(ByVal ByteArray() As Byte) Handles Input.ReadComplete
RTB.Text = System.Text.Encoding.UTF8.GetString(ByteArray)
End Sub
If you can tweak it, can you post it please?
-
Mar 2nd, 2003, 03:11 PM
#8
Thread Starter
Lively Member
Thanks you guys.
[LGS]Static, your code with a little tweaking works like a charm, thank you
-
Mar 3rd, 2003, 09:37 PM
#9
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"
-
Mar 3rd, 2003, 10:09 PM
#10
Thread Starter
Lively Member
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:
sr = File.OpenText(fn)
Dim x As String = sr.ReadToEnd
sr.Close()
txtDisplay.Lines = Split(x, vbLf)
Thanks again
-
Mar 4th, 2003, 10:08 AM
#11
Junior Member
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.
-
Mar 4th, 2003, 05:14 PM
#12
Thread Starter
Lively Member
It is an array Guile, x gets all the text from the file them splits it into an array.
-
Mar 5th, 2003, 08:29 AM
#13
Junior Member
My Initial Code:
VB Code:
opnFile.ShowDialog()
Dim fn As String = opnFile.FileName
Dim sr As New StreamReader(fn)
Dim x As String = sr.ReadToEnd()
sr.Close()
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:
sr = File.OpenText(fn)
Dim x As String = sr.ReadToEnd
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:
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.
-
Mar 5th, 2003, 10:52 AM
#14
Thread Starter
Lively Member
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.
-
Mar 5th, 2003, 12:58 PM
#15
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"
-
Mar 5th, 2003, 02:03 PM
#16
Thread Starter
Lively Member
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)
-
Mar 5th, 2003, 02:12 PM
#17
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"
-
Mar 5th, 2003, 02:19 PM
#18
Thread Starter
Lively Member
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
-
Mar 5th, 2003, 07:13 PM
#19
Junior Member
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.
-
Mar 5th, 2003, 10:17 PM
#20
Junior Member
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()
-
Mar 5th, 2003, 10:27 PM
#21
Thread Starter
Lively Member
Guile, it is set to multiline purposely
-
Mar 6th, 2003, 03:13 AM
#22
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|