473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

StringReader: How to determinethe number of lines before using ReadLine

How does one determine the number of lines before using ReadLine.
This would enable the ReDim to be out of the loop.

sReader = New StringReader(xxx.GetObjectText_)
If Not (sReader Is Nothing) Then
i = -1
Do Until sReader.Peek = -1
i = i + 1
ReDim Preserve yyy(i) ' yyy is typed as String
yyy(i) = sReader.ReadLine
Loop
End If

--
http://www.standards.com/; See Howard Kaikow's web site.
Nov 21 '05 #1
5 7012
You could do a couple different things. One is to run through the file but
don't save the contents to get a count.

Do Until sReader.Peek = -1
i = i + 1
sReader.ReadLine
Loop

Depending what you are doing with the data after you could use an array
list, it allows you to grow an array as you need it.

Dim MyArray as new Arraylist
Do Until sReader.Peek = -1
i = i + 1
MyArray.Add(sReader.ReadLine)
Loop

You could also just read the file in all at once.

Dim S as String = sReader.ReadToEnd()

Hope it helps
"Howard Kaikow" <ka****@standards.com> wrote in message
news:uL**************@TK2MSFTNGP15.phx.gbl...
How does one determine the number of lines before using ReadLine.
This would enable the ReDim to be out of the loop.

sReader = New StringReader(xxx.GetObjectText_)
If Not (sReader Is Nothing) Then
i = -1
Do Until sReader.Peek = -1
i = i + 1
ReDim Preserve yyy(i) ' yyy is typed as String
yyy(i) = sReader.ReadLine
Loop
End If

--
http://www.standards.com/; See Howard Kaikow's web site.

Nov 21 '05 #2
"Chris" <No****@NoSpam.com> wrote in message
news:OX**************@TK2MSFTNGP14.phx.gbl...
You could do a couple different things. One is to run through the file but don't save the contents to get a count.

Do Until sReader.Peek = -1
i = i + 1
sReader.ReadLine
Loop

Depending what you are doing with the data after you could use an array
list, it allows you to grow an array as you need it.

Dim MyArray as new Arraylist
Do Until sReader.Peek = -1
i = i + 1
MyArray.Add(sReader.ReadLine)
Loop

You could also just read the file in all at once.

Dim S as String = sReader.ReadToEnd()

Hope it helps


Thanx.

I am using ReDim Preserve to grow the array as the lines are read, but that
is not very efficient.
I had hoped that there was a count property somewhere.

In this app, the arrays are rather small, so the overhead of ReDim Preserve
should not be noticeable.
For larger arrays, the choices seem to be to make a separate pass to count
the lines, or to grow the array with ArrayList. Not sure which would be
faster.

In any case, I do not want to use Arraylists instead of normal arrays,
unless I can see an advantage.
--
http://www.standards.com/; See Howard Kaikow's web site.
Nov 21 '05 #3

"Howard Kaikow" <ka****@standards.com> wrote in message
news:eo**************@tk2msftngp13.phx.gbl...
"Chris" <No****@NoSpam.com> wrote in message
news:OX**************@TK2MSFTNGP14.phx.gbl...
You could do a couple different things. One is to run through the file

but
don't save the contents to get a count.

Do Until sReader.Peek = -1
i = i + 1
sReader.ReadLine
Loop

Depending what you are doing with the data after you could use an array
list, it allows you to grow an array as you need it.

Dim MyArray as new Arraylist
Do Until sReader.Peek = -1
i = i + 1
MyArray.Add(sReader.ReadLine)
Loop

You could also just read the file in all at once.

Dim S as String = sReader.ReadToEnd()

Hope it helps


Thanx.

I am using ReDim Preserve to grow the array as the lines are read, but
that
is not very efficient.
I had hoped that there was a count property somewhere.

In this app, the arrays are rather small, so the overhead of ReDim
Preserve
should not be noticeable.
For larger arrays, the choices seem to be to make a separate pass to count
the lines, or to grow the array with ArrayList. Not sure which would be
faster.

In any case, I do not want to use Arraylists instead of normal arrays,
unless I can see an advantage.
--
http://www.standards.com/; See Howard Kaikow's web site.


Redim Preserve for an smallish array of value types is really not that
expensive since the array only has a pointer for each member. The main
advantage of an ArrayList is that it will do fewer "redim"s. The
ArrayList's internal array will grow by 50% each time, or some such clever
ratio designed to strike a balance between wasted space and excessive
copying.

You coule do the same thing with

Do Until sReader.Peek = -1
i = i + 1
if i > UBound(yyy) then
ReDim Preserve yyy((i*3)\2)
end if
yyy(i) = sReader.ReadLine
Loop
ReDim Preserve yyy(i)

David
Nov 21 '05 #4
I would make a first guess at the size then check the no. of lines in the
loop and increment the dimension using preserve by 25 or 50 or whatever then
when done, do a final redim preserve to the no. of lines. Of course,
Arraylists are faster and you can always copy the arraylist into an array
when the loop is finished.

"Chris" wrote:
You could do a couple different things. One is to run through the file but
don't save the contents to get a count.

Do Until sReader.Peek = -1
i = i + 1
sReader.ReadLine
Loop

Depending what you are doing with the data after you could use an array
list, it allows you to grow an array as you need it.

Dim MyArray as new Arraylist
Do Until sReader.Peek = -1
i = i + 1
MyArray.Add(sReader.ReadLine)
Loop

You could also just read the file in all at once.

Dim S as String = sReader.ReadToEnd()

Hope it helps
"Howard Kaikow" <ka****@standards.com> wrote in message
news:uL**************@TK2MSFTNGP15.phx.gbl...
How does one determine the number of lines before using ReadLine.
This would enable the ReDim to be out of the loop.

sReader = New StringReader(xxx.GetObjectText_)
If Not (sReader Is Nothing) Then
i = -1
Do Until sReader.Peek = -1
i = i + 1
ReDim Preserve yyy(i) ' yyy is typed as String
yyy(i) = sReader.ReadLine
Loop
End If

--
http://www.standards.com/; See Howard Kaikow's web site.


Nov 21 '05 #5
Howard,

Because I see where you need that value for in this messagethread, than is
my answer that I would just read it and Add the strings to an Arraylist.
An Arraylist is a collection of references to strings (or whatever) that are
build anyway when you are reading.

Just my thought,

Cor
Nov 21 '05 #6

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

Similar topics

6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
4
by: Vannela | last post by:
I am using stringreader to repeat through each line of a file ,but my string reader is clubbing 4 to 5 lines as single line at few places, what could be the problem? Please send me the...
3
by: Mike | last post by:
Is it true that these objects are forwardonly? If not, can someone show me how to go back to line 1, after I have read the first 100 lines. Thanks Mike
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
2
by: Eddy | last post by:
I have a big problem with streamreader ReadLine()! I read from a long text files about 13k lines, than I encounter a problem: ReadLine() is not anymore able to go on! I have a string whose name is...
4
by: somequestion | last post by:
Question 1. i am using StreamReader Class like this... string str = string.Empty; StreamReader sr = new StreamReader(fsIn,Encoding.Default) while ((str = sr.ReadLine()) != null) { // wanna get...
3
by: PointMan | last post by:
what i know is... while ((currentStr = sr.ReadLine()) != null) { in this area, can i get next ReadLine Value of currentStr } best regard,,
37
by: shana07 | last post by:
Hi..can I ask one question about readLine()? By having such below codes, I can get it to read first 200 lines out of 500 lines in an output file. for(int ind=0; ind<200;ind++) { ...
0
by: 7stud | last post by:
Hi, 1) Does this make any sense: """ Thus, the loop: for line in f: iterates on each line of the file. Due to buffering issues,
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.