473,671 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(xx x.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.ReadLin e
Loop
End If

--
http://www.standards.com/; See Howard Kaikow's web site.
Nov 21 '05 #1
5 7035
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.ReadLin e
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(sRe ader.ReadLine)
Loop

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

Dim S as String = sReader.ReadToE nd()

Hope it helps
"Howard Kaikow" <ka****@standar ds.com> wrote in message
news:uL******** ******@TK2MSFTN GP15.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(xx x.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.ReadLin e
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******** ******@TK2MSFTN GP14.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.ReadLin e
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(sRe ader.ReadLine)
Loop

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

Dim S as String = sReader.ReadToE nd()

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****@standar ds.com> wrote in message
news:eo******** ******@tk2msftn gp13.phx.gbl...
"Chris" <No****@NoSpam. com> wrote in message
news:OX******** ******@TK2MSFTN GP14.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.ReadLin e
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(sRe ader.ReadLine)
Loop

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

Dim S as String = sReader.ReadToE nd()

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.ReadLin e
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.ReadLin e
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(sRe ader.ReadLine)
Loop

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

Dim S as String = sReader.ReadToE nd()

Hope it helps
"Howard Kaikow" <ka****@standar ds.com> wrote in message
news:uL******** ******@TK2MSFTN GP15.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(xx x.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.ReadLin e
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
4274
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 while statement checking for an empty string "" which I understand represents an EOF in Python. The text file has some blank lines with spaces and other with blanks. Thanks a lot.
4
3155
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 suggestions Thank you
3
2752
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
13077
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 server is on one line (\r\n at end) and is formed as - each of which is seperated by a space. Arguments with spaces in them are enclosed in quotations. So, I'm able to open a connection to the server. When I send a message to
2
8489
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 riga, where I put the return value of Readline(), and this is what happens, when I read these three lines: 106933;;rosso;ivan mauricio;3347614603;trak15@yahoo.com;1984-06-04;M;;;milano;CO;politecnico # ingegneria;;;...
4
17914
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 line Count of Sr } i just want to get a count of sr.
3
1816
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
7602
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++) { String lineA = ""; lineA = inputA.readLine(); How to start readLine() at line number 200 until 500 then? Kindly please advisme how .....Thank you in advance.
0
4712
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
8930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8605
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8677
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6238
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5704
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4417
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
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 we have to send another system
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.