Evoluator

Hi guys;

I am trying to do this in my application but it is not working.
I am trying to read certain number of characters in a text from a text file stored in the uers HDD, then after reading them read the next block and continue untill it reaches then end. But it has proven to be harder than it seems like.

I have tried the following;


But doesn't work could some body help me please

Code Snippet

Dim StreamRead As System.IO.StreamReader
Dim j As Integer
Dim value As String

StreamRead = My.Computer.FileSystem.OpenTextFileReader("C:\Text.txt")

While j <= 251

value = value & StreamRead.Read()

j += 1

End While 'but the Read() is an integer type!! What could i do to only
'read 251 characters from the text







Re: Visual Basic Express Edition How could I read 300 characters from a text file?

Evoluator

This is quiet cool, i asked the question but before any help was given to me I fiqured out a solution for it!

To read a certain length of characters in any file I could do the following:

Code Snippet


' This creats an array from the specified file, in this case "C:\Text.Txt".
Dim buffering() = My.Computer.FileSystem.ReadAllText("C:\Text.Txt").ToArray

Dim i as integer


While i <= 250

value = value & buffering(i).ToString

i += 1

End while


' The variable `value` would contain the first 250 characters
' of the file which in this case is "C:\Text.Txt".







Then every time the next 250 or any other integer value of characters are required you could add the above While loop statment to obtain the next block of code. (The block of code could vary in this example it was 250, but it could be any thing you like, at every while loop statment)

But insure that you don't change the value of ' i '.










Re: Visual Basic Express Edition How could I read 300 characters from a text file?

js06

I thought i would post this different example for anyone who wants to see something similar where it populates a richtextbox with the entire file and then the first 250 into a second box

i hope you don't mind

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim textstring As String

Dim opendlg As New OpenFileDialog

opendlg.Filter = "Text Files (*.txt|*.txt|All Files (*.*)|*.*"

If opendlg.ShowDialog() = Windows.Forms.DialogResult.OK Then

textstring = My.Computer.FileSystem.ReadAllText(opendlg.FileName)

Me.RichTextBox1.Text = textstring

Me.RichTextBox2.Text = textstring.Substring(0, 250)

End If

End Sub






Re: Visual Basic Express Edition How could I read 300 characters from a text file?

Evoluator

Of course i don't mind!

By the way I have learned a new code from your sample code thanks for your help!
You see I am not an experienced programmer I am only 17 years old, so I need a bit of help with basic programming every one and then Wink, thanks for your help.





Re: Visual Basic Express Edition How could I read 300 characters from a text file?

js06

I am fairly new to all this myself

One question though

I got an error on the first line when i tried your code

(toarray is not a member of string)

How did you get the code to work

Did you make any other changes






Re: Visual Basic Express Edition How could I read 300 characters from a text file?

Evoluator


This is my original code, I have used a Try statment so that when the file size is smaller than 250 bytes it would add " " characters to the end of the variable.

This code is working fine for me!


Dim buffering() = My.Computer.FileSystem.ReadAllText(Form1.TextBox1.Text).ToArray
Dim i As Integer
Dim value


While i <= 250
Try
value = value & buffering(i).ToString

Catch ex As Exception

value = value & ""

End Try

i += 1
End While

MsgBox(value.ToString)