Connecting Tech Pros Worldwide Forums | Help | Site Map

reading file into array

James
Guest
 
Posts: n/a
#1: Nov 23 '05
i've a text file in the following format and i would like to read into this
array

text file
======
machine1
machine2

Dim sr As StreamReader = New StreamReader("machines.txt")
dim line as string
Do

line = sr.ReadLine



Loop Until line Is Nothing

sr.Close()

How can i read line by line into this form of array :

dim strcomputer() as string = {"machine1", "machine2"}











Cor Ligthert [MVP]
Guest
 
Posts: n/a
#2: Nov 23 '05

re: reading file into array


James,

By not using the fixed array however the dynamic arraylist. (see inline 2
places in the code)

dim myArrayList as new Arraylist[color=blue]
> Dim sr As StreamReader = New StreamReader("machines.txt")
> dim line as string
> Do
>
> line = sr.ReadLine[/color]
myArrayList.Add(line)[color=blue]
>
>
> Loop Until line Is Nothing
>
> sr.Close()[/color]

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor


James
Guest
 
Posts: n/a
#3: Nov 23 '05

re: reading file into array


dim myArrayList as new Arraylist (does not allow me to define using
the NEW word ..pls advise

etc
etc

myArrayList.Add(line)



"Cor Ligthert [MVP]" <notmyfirstname@planet.nl> wrote in message
news:OEC3NsQ4FHA.1864@TK2MSFTNGP12.phx.gbl...[color=blue]
> James,
>
> By not using the fixed array however the dynamic arraylist. (see inline 2
> places in the code)
>
> dim myArrayList as new Arraylist[color=green]
>> Dim sr As StreamReader = New StreamReader("machines.txt")
>> dim line as string
>> Do
>>
>> line = sr.ReadLine[/color]
> myArrayList.Add(line)[color=green]
> >
>>
>> Loop Until line Is Nothing
>>
>> sr.Close()[/color]
>
> http://msdn.microsoft.com/library/de...classtopic.asp
>
> I hope this helps,
>
> Cor
>[/color]


James
Guest
 
Posts: n/a
#4: Nov 23 '05

re: reading file into array


sorry pls ignore my last question as it was a typo error


"James" <jkklim@hotmail.com> wrote in message
news:OcP2gYR4FHA.696@TK2MSFTNGP09.phx.gbl...[color=blue]
> dim myArrayList as new Arraylist (does not allow me to define using
> the NEW word ..pls advise
>
> etc
> etc
>
> myArrayList.Add(line)
>
>
>
> "Cor Ligthert [MVP]" <notmyfirstname@planet.nl> wrote in message
> news:OEC3NsQ4FHA.1864@TK2MSFTNGP12.phx.gbl...[color=green]
>> James,
>>
>> By not using the fixed array however the dynamic arraylist. (see inline 2
>> places in the code)
>>
>> dim myArrayList as new Arraylist[color=darkred]
>>> Dim sr As StreamReader = New StreamReader("machines.txt")
>>> dim line as string
>>> Do
>>>
>>> line = sr.ReadLine[/color]
>> myArrayList.Add(line)[color=darkred]
>> >
>>>
>>> Loop Until line Is Nothing
>>>
>>> sr.Close()[/color]
>>
>> http://msdn.microsoft.com/library/de...classtopic.asp
>>
>> I hope this helps,
>>
>> Cor
>>[/color]
>
>[/color]


Claes Bergefall
Guest
 
Posts: n/a
#5: Nov 23 '05

re: reading file into array


You could also use a StringCollection instead of ArrayList.
Depends on what you want to do with the data once
you're read it

/claes

"Cor Ligthert [MVP]" <notmyfirstname@planet.nl> wrote in message
news:OEC3NsQ4FHA.1864@TK2MSFTNGP12.phx.gbl...[color=blue]
> James,
>
> By not using the fixed array however the dynamic arraylist. (see inline 2
> places in the code)
>
> dim myArrayList as new Arraylist[color=green]
> > Dim sr As StreamReader = New StreamReader("machines.txt")
> > dim line as string
> > Do
> >
> > line = sr.ReadLine[/color]
> myArrayList.Add(line)[color=green]
> >
> >
> > Loop Until line Is Nothing
> >
> > sr.Close()[/color]
>
>[/color]
http://msdn.microsoft.com/library/de...classtopic.asp[color=blue]
>
> I hope this helps,
>
> Cor
>
>[/color]


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#6: Nov 23 '05

re: reading file into array


"James" <jkklim@hotmail.com> schrieb:[color=blue]
> i've a text file in the following format and i would like to read into
> this array
>
> text file
> ======
> machine1
> machine2[/color]

\\\
Imports System.IO
..
..
..
Dim Reader As New StreamReader("C:\WINDOWS\WIN.INI")
Dim Lines() As String = _
Split(Reader.ReadToEnd(), ControlChars.NewLine)
Reader.Close()
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Closed Thread


Similar Visual Basic .NET bytes