reading file into array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James

    reading file into array

    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("m achines.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]

    #2
    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("m achines.txt")
    > dim line as string
    > Do
    >
    > line = sr.ReadLine[/color]
    myArrayList.Add (line)[color=blue]
    >
    >
    > Loop Until line Is Nothing
    >
    > sr.Close()[/color]



    I hope this helps,

    Cor


    Comment

    • James

      #3
      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:OEC3NsQ4FH A.1864@TK2MSFTN GP12.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("m achines.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]


      Comment

      • James

        #4
        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:OcP2gYR4FH A.696@TK2MSFTNG P09.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:OEC3NsQ4FH A.1864@TK2MSFTN GP12.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("m achines.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]


        Comment

        • Claes Bergefall

          #5
          Re: reading file into array

          You could also use a StringCollectio n 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:OEC3NsQ4FH A.1864@TK2MSFTN GP12.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("m achines.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]


          Comment

          • Herfried K. Wagner [MVP]

            #6
            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.I NI")
            Dim Lines() As String = _
            Split(Reader.Re adToEnd(), ControlChars.Ne wLine)
            Reader.Close()
            ///

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

            Comment

            Working...