473,503 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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?

Apr 6 '06 #1
6 5930

at*****@gmail.com wrote:
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]

Apr 6 '06 #2
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

Apr 7 '06 #3
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?
<at*****@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.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

Apr 7 '06 #4

at*****@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]

Apr 7 '06 #5

Stephany Young wrote:
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]

Apr 7 '06 #6

<at*****@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.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?


Does VB 2005 not still support Binary opens?

Apr 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
42983
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number...
21
6198
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
2
1940
by: Chris Michael | last post by:
Hello everybody, Newbie here. I've been working on this for the last two days and I can't figure out where this problem is. I think it's something so obvious, but I can't see it! OK, firstly...
75
5274
by: Greg McIntyre | last post by:
I have a Python snippet: f = open("blah.txt", "r") while True: c = f.read(1) if c == '': break # EOF # ... work on c Is some way to make this code more compact and simple? It's a bit...
40
61164
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
1
3979
by: Jose Reckoner | last post by:
I'm running python 2.3 on Windows XP. Anyone have a quick small script to convert .DT1 and .DEM data to ASCII or some other format? I don't need a viewer. Thanks!
10
1779
by: Solo.Wolve | last post by:
Well,I read <c primer plusbefore,and I just got a primary conclusion about c. Can somebody show me some books to read and something to do ? I study c myself.btw,I am not a student, Thanks.
8
2143
by: andrew.jefferies | last post by:
Hi, I'm trying to write a simple log parsing program. I noticed that it isn't reading my log file to the end. My log is around 200,000 lines but it is stopping at line 26,428. I checked that...
13
10374
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.