Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read a set number of bytes using streamreader 1

Status
Not open for further replies.

Swi

Programmer
Joined
Feb 4, 2002
Messages
1,981
Location
US
I am new to VB.NET. In VB6 I used to be able to use the FSO and sr.Read(numofbytes). How would I go about that in VB.NET. I get errors when I use sr.Read(numofbytes). Thanks in advance.


Do Until sr.Peek = -1
Counter = Counter + 1
Progressbar1.Value = Counter
InputData = sr.ReadLine()
sw.WriteLine (InputData.Substring(4,2))
Loop

Swi
 
You can read a set number of characters into a character array buffer, if that helps.

Code:
Dim buff As Char()  'will contain results of read
sr.Read(buff, 0, 5)
 
The 0 and 5 are index and count to read, respectively.
 
Thanks for the reply. I am still getting an error. They weren't kidding when they said .NET is alot different than VB6. :)

Private Sub Button1Click(sender As System.Object, e As System.EventArgs)
Dim Counter As Long
Dim sr As StreamReader
Dim sw As StreamWriter
Dim InputData As String
Dim RecSize As Integer
Dim buff As Char()

sr = New StreamReader(txtInputFile.Text)
sw = New StreamWriter(txtReportFile.text)
objFileInfo = New System.IO.FileInfo(txtInputFile.Text)
RecSize = CInt(txtRecSize.Text)
Counter = 0

With Progressbar1
.Visible = true
.Minimum = 1
.Maximum = (objFileInfo.Length / RecSize)
End With

Do Until sr.Peek = -1
Counter = Counter + 1
Progressbar1.Value = Counter
InputData = sr.Read(buff,0,RecSize)
sw.WriteLine (InputData.Substring(4,2))
Loop

sr.Close()
sr = Nothing
sw.Close()
sw = Nothing

MessageBox.Show("Process Complete!", "MyFirstProj", _
MessageBoxButtons.OK, MessageBoxIcon.Information, _
MessageBoxDefaultButton.Button1)
End Sub

Swi
 
That's because the Read method doesn't return that character array - it just returns the 32 bit integer representation of the next character after it's done reading.

It looks like you're just trying to read in a text file - what about InputData = sr.ReadToEnd()? The downside is that you wouldnt' get your progress bar.

How big are the text files you're reading? You should see a significant speed improvement with .NET: if the files are small you won't need a progress bar.

If you still need the bar, you could also do something like the following (don't quote me, this is off the top of my head):

Code:
Do Until sr.Peek = -1
  Progressbar1.Value += 1
  InputData &= Chr(sr.Read()).toString()
  sw.WriteLine (InputData.Substring(4,2))
Loop

-Every call to sr.Read should return the next character and advance the position in the file by one character.
 
You are correct, I am just trying to read the text file. I would like to have the progress bar as the files can be millions of records (2+ gig). The reason I want to read a set amount of bytes is that some files are ascii but some files are ebcdic as well (I have a translation routine that I need to convert from VB6). I trid the below code but it still did not work.

InputData &= Chr(sr.Read()).toString()

Swi
 
Hm, could you paste your exact code then?

I tried:

Code:
Dim sr As New IO.StreamReader("c:\testfile.txt")
Dim s As String

While sr.Peek <> -1
    s &= Chr(sr.Read()).toString()
End While

MessageBox.Show(s)

-it read each character one by one and worked fine.
 
It gives me the error 'Chr' is not declared.

Swi
 
Try Microsoft.VisualBasic.Chr(...).
 
Or, I suppose a more .NET-esque way would be to use Convert.ToChar(...).
 
Got it working using this. Is there a more eloquent way to achieve what I want to do?

Private Sub Button1Click(sender As System.Object, e As System.EventArgs)
Dim Counter As Long
Dim sr As StreamReader
Dim sw As StreamWriter
Dim RecSize As Integer
Dim InputData() As Char

sr = New StreamReader(txtInputFile.Text)
sw = New StreamWriter(txtReportFile.Text)
objFileInfo = New System.IO.FileInfo(txtInputFile.Text)
RecSize = CInt(txtRecSize.Text)
ReDim InputData(RecSize)
Counter = 0

With Progressbar1
.Visible = true
.Minimum = 1
.Maximum = (objFileInfo.Length / RecSize)
End With

Do Until sr.Peek = -1
Counter = Counter + 1
Progressbar1.Value = Counter
sr.Read(InputData, 0, InputData.Length-1)
sw.WriteLine (Microsoft.VisualBasic.Mid( _
Inputdata,5,2))
Loop

sr.Close()
sr = Nothing
sw.Close()
sw = Nothing

MessageBox.Show("Process Complete!", "MyFirstProj", _
MessageBoxButtons.OK, MessageBoxIcon.Information, _
MessageBoxDefaultButton.Button1)
End Sub

Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top