Home All Groups Group Topic Archive Search About

Can the read() function in VB.NET start reading from somewhere else than 0

Author
6 Apr 2006 10:01 PM
ataanis
Hi,
Can anybody tell me , why if I change the second value in the read()
function, I get an error message, I'm dealing here with a file, that
has at least 240 caracters per line

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            Dim sr As New
System.IO.StreamReader(OpenFileDialog1.FileName)
            Dim line As String
            line = sr.ReadLine()
            Dim b(224) As Char
            sr.Read(b, 0, 224)
            MessageBox.Show(b)
            sr.Close()
        End If
    End Sub

BASICALLY , When I change the 0 to something else, it doesn't work ,
why?

Author
6 Apr 2006 10:47 PM
Tom Shelton
ataa***@gmail.com wrote:
Show quoteHide quote
> Hi,
> Can anybody tell me , why if I change the second value in the read()
> function, I get an error message, I'm dealing here with a file, that
> has at least 240 caracters per line
>
> Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MenuItem2.Click
>         If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
>             Dim sr As New
> System.IO.StreamReader(OpenFileDialog1.FileName)
>             Dim line As String
>             line = sr.ReadLine()
>             Dim b(224) As Char
>             sr.Read(b, 0, 224)
>             MessageBox.Show(b)
>             sr.Close()
>         End If
>     End Sub
>
> BASICALLY , When I change the 0 to something else, it doesn't work ,
> why?

If your trying to read from a different position in the file - then not
really. You can't arbitrarily move the file pointer on a StreamReader.
You can read a chunk up to the position you want, and then read....

The second parameter to StreamReader.Read is NOT a file position.  It
is the position (index) in the buffer (b in your case) that you want to
start writting data to.

If you want to seek in the file (position the file pointer) then you
will need to use a System.IO.FileStream instead.  Look at it's Seek
method.

--
Tom Shelton [MVP]
Author
7 Apr 2006 1:17 AM
ataanis
I just realized the SEEK method has the same problem, from what I
understand, it sets the file pointer to the beginning of the file, and
can't be changed!!! I need a way to set the streamreader to a certain
place in the line, I already know the column number  , am I right that
seek is not the right thing to use here?
thanks
Author
7 Apr 2006 2:12 AM
Stephany Young
Well, you haven't really given enough information.

We know that, in whatever you are attempting to read, there are 'lines'
containing at least 240 characters. We also know that you either want to do
something with the the first 225 characters in each line or you want to
ignore the first 225 characters in each line.

What we dont know is what constitutes a line and we don't know whether or
not you referring to a 0 or 1 based index.

For the purpose of the exercise I will assume that 'lines' are seperated by
a CrLf combination and that you want to ignore the first 225 characters on
each line.

Try this, or something like it:

  Dim _line As String
  Dim _the_part_we_want As String

  'Open the streamreader

  _line = _sr.ReadLine()

  While _line IsNot Nothing
    _the_part_we_want = _line.SubString(225)
    ...
    ' Do whatever you want with _the_part_we_want
    ...
    _line = _sr.ReadLine()
  End While

  'Close the streamreader

Is that simple or what?


<ataa***@gmail.com> wrote in message
Show quoteHide quote
news:1144372646.296778.195900@u72g2000cwu.googlegroups.com...
>I just realized the SEEK method has the same problem, from what I
> understand, it sets the file pointer to the beginning of the file, and
> can't be changed!!! I need a way to set the streamreader to a certain
> place in the line, I already know the column number  , am I right that
> seek is not the right thing to use here?
> thanks
>
Author
7 Apr 2006 2:47 AM
Tom Shelton
Stephany Young wrote:
Show quoteHide quote
> Well, you haven't really given enough information.
>
> We know that, in whatever you are attempting to read, there are 'lines'
> containing at least 240 characters. We also know that you either want to do
> something with the the first 225 characters in each line or you want to
> ignore the first 225 characters in each line.
>
> What we dont know is what constitutes a line and we don't know whether or
> not you referring to a 0 or 1 based index.
>
> For the purpose of the exercise I will assume that 'lines' are seperated by
> a CrLf combination and that you want to ignore the first 225 characters on
> each line.
>
> Try this, or something like it:
>
>   Dim _line As String
>   Dim _the_part_we_want As String
>
>   'Open the streamreader
>
>   _line = _sr.ReadLine()
>
>   While _line IsNot Nothing
>     _the_part_we_want = _line.SubString(225)
>     ...
>     ' Do whatever you want with _the_part_we_want
>     ...
>     _line = _sr.ReadLine()
>   End While
>
>   'Close the streamreader
>
> Is that simple or what?
>

Stephany - very good.  That is a very simple solution if this is indeed
a line delimited file.  I think, you are also correct - we need to get
more information about this file.

--
Tom Shelton [MVP]
Author
7 Apr 2006 2:42 AM
Tom Shelton
ataa***@gmail.com wrote:
> I just realized the SEEK method has the same problem, from what I
> understand, it sets the file pointer to the beginning of the file, and
> can't be changed!!! I need a way to set the streamreader to a certain
> place in the line, I already know the column number  , am I right that
> seek is not the right thing to use here?
> thanks

Seek lets you move the file pointer to any byte position in a file
realtive to an origin - either the begining (byte 0), the current file
byte position, or the end of the file.  There are examples in the
documentation, but if you know that you need to move to byte 15 of the
first line of the file then it would be something like

fs.Seek (14, SeekOrigin.Begin)

At that point, you would issue your read:

fs.Read (buffer, 0, buffer.length)

When you need to move it again, then you can call Seek to the next
position in the file where you want to begin reading.

--
Tom Shelton [MVP]