Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Reading the contents of a file into an array

Reading the contents of a file into an array

Scheduled Pinned Locked Moved Visual Basic
data-structureshelpquestion
17 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G GeorgieMPorgie

    Thanks a lot for the feedback (and education). This is so much easier than what I was doing. :thumbsup:

    George

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #7

    I'm afraid you still haven't figured out why it failed the way you did it. You opened a file, then had a loop reading all its contents (till Peek fails), en then another loop supposed to read all content again. However you never told the file it should restart at the beginning (the again was in your mind, not in your code), so your second loop starts at the end of the file (where it had left off when the first loop ended) and then obviously finds nothing more. To restart a file, either close and open it again, or simply set its Position to zero. So either do it yourself (as you tried) but do it correctly, or use what is available to tackle the most popular situations, such as File.ReadAllLines(). Quite often, less code also means fewer bugs. :)

    Luc Pattyn [My Articles] Nil Volentibus Arduum

    G 1 Reply Last reply
    0
    • L Luc Pattyn

      I'm afraid you still haven't figured out why it failed the way you did it. You opened a file, then had a loop reading all its contents (till Peek fails), en then another loop supposed to read all content again. However you never told the file it should restart at the beginning (the again was in your mind, not in your code), so your second loop starts at the end of the file (where it had left off when the first loop ended) and then obviously finds nothing more. To restart a file, either close and open it again, or simply set its Position to zero. So either do it yourself (as you tried) but do it correctly, or use what is available to tackle the most popular situations, such as File.ReadAllLines(). Quite often, less code also means fewer bugs. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      G Offline
      G Offline
      GeorgieMPorgie
      wrote on last edited by
      #8

      Thank you for explaining what my error was because (as you mention) I never did understand why it didn't work. I actually rewrote my program using file.readalllines() and used a list instead of an array. Thanks again for helping out. Not only did I get an answer to my original question, but I actually learned a better way to get my desired result.

      George

      1 Reply Last reply
      0
      • L Lost User

        GeorgieMPorgie wrote:

        I'm curious as to why what I'm doing isn't working?

        What does the computer say when you try to run it? Something about your reader?

        GeorgieMPorgie wrote:

        I'll look them up.

        Just curious, did you? The example[^] in the documentation on the method that Luc pointed you to actually shows that it can be done in a single line;

        Dim readText() As String = File.ReadAllLines(path)

        Bastard Programmer from Hell :suss:

        G Offline
        G Offline
        GeorgieMPorgie
        wrote on last edited by
        #9

        Eddy, I didn't get an error result and no message about my reader. Yes I did look up lists and file.readalllines(). Learned a lot today from some very kind people. I'll probably end up rewriting a few other programs using what I learned.

        George

        L 1 Reply Last reply
        0
        • G GeorgieMPorgie

          Eddy, I didn't get an error result and no message about my reader. Yes I did look up lists and file.readalllines(). Learned a lot today from some very kind people. I'll probably end up rewriting a few other programs using what I learned.

          George

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #10

          Cool :thumbsup:

          Bastard Programmer from Hell :suss:

          1 Reply Last reply
          0
          • L Luc Pattyn

            is this VB6 or VB.NET? how many times can you "read to the end"? why do you read the file twice? you could extract and store the required data and find out the number of lines in one go (and your problem would suddenly disappear). if VB.NET, why don't you use a List of Something rather than an array (whose dimension is an implicit limitation to your program)? And are you familiar with File.ReadAllText? File.ReadAllLines? :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            P Offline
            P Offline
            proneal
            wrote on last edited by
            #11

            Dim stocksymbolarrayLong() As String
            Dim filecount As Integer
            'Count the lines in the file you are reviewing.

            Dim LongFile As System.IO.StreamReader
            LongFile = System.IO.File.OpenText("C:\Users\George Desktop\Documents\Stock System\Stock Programs\Stock Program Data\LongStockTesting.txt")

            filecount = 0

            Do Until LongFile.Peek = -1
            LongFile.ReadLine()
            ReDim Preserve stocksymbolarrayLong(filecount)
            stocksymbolarrayLong(filecount) = LongFile.ReadLine
            filecount = filecount + 1
            Loop

            LongFile.Close()
            ' Sort the array's
            If Not stocksymbolarrayLong Is Nothing Then
            Array.Sort(stocksymbolarrayLong)
            End If

            I did a little reworking USING your method of getting the values. There is better one, readALlLines from the IO.File class, but I used ur code. This should work , but I can't test it. Good luck!

            L 1 Reply Last reply
            0
            • P proneal

              Dim stocksymbolarrayLong() As String
              Dim filecount As Integer
              'Count the lines in the file you are reviewing.

              Dim LongFile As System.IO.StreamReader
              LongFile = System.IO.File.OpenText("C:\Users\George Desktop\Documents\Stock System\Stock Programs\Stock Program Data\LongStockTesting.txt")

              filecount = 0

              Do Until LongFile.Peek = -1
              LongFile.ReadLine()
              ReDim Preserve stocksymbolarrayLong(filecount)
              stocksymbolarrayLong(filecount) = LongFile.ReadLine
              filecount = filecount + 1
              Loop

              LongFile.Close()
              ' Sort the array's
              If Not stocksymbolarrayLong Is Nothing Then
              Array.Sort(stocksymbolarrayLong)
              End If

              I did a little reworking USING your method of getting the values. There is better one, readALlLines from the IO.File class, but I used ur code. This should work , but I can't test it. Good luck!

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #12

              Did you send that to the intended person? I wasn't expecting any help, I was only offering some. :confused:

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              P 2 Replies Last reply
              0
              • L Luc Pattyn

                Did you send that to the intended person? I wasn't expecting any help, I was only offering some. :confused:

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                P Offline
                P Offline
                proneal
                wrote on last edited by
                #13

                Yes, I am sorry -it looks like I replied under wrong heading. My post is intended for OP, and not you in any way. Y_our resolution is what I would have done too, btw_ ;) .Thanks for letting me clear this up. :) have a Great Day Luc!

                1 Reply Last reply
                0
                • L Luc Pattyn

                  Did you send that to the intended person? I wasn't expecting any help, I was only offering some. :confused:

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  P Offline
                  P Offline
                  proneal
                  wrote on last edited by
                  #14

                  Well, who I was meaning to refer to regarding readALLLines method-was the poster that offered up the ReadALlLines method of the File class. - Still, you offered a great help explaining why OP code was failing. That was a good response. :)

                  L 1 Reply Last reply
                  0
                  • P proneal

                    Well, who I was meaning to refer to regarding readALLLines method-was the poster that offered up the ReadALlLines method of the File class. - Still, you offered a great help explaining why OP code was failing. That was a good response. :)

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #15

                    Thanks. :)

                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                    1 Reply Last reply
                    0
                    • G GeorgieMPorgie

                      I'm trying to read the contents of a file into an array, but for some reason the values are showing up in the array as nothing? The contents of the file look like this (the file length is about 5,690 lines long)

                      AA.CSV
                      AAAGY.CSV
                      AABC.CSV
                      AACB.CSV

                      This is the section of code I have written to attempt to do this

                      'Read the stock symbols to be copied to a new folder into an array

                          Dim stocksymbolarrayLong(25000) As String
                          Dim filecount As Integer
                          Dim counter As Integer
                      
                          'Count the lines in the file you are reviewing.
                      
                          Dim LongFile As System.IO.StreamReader
                          LongFile = System.IO.File.OpenText("C:\\Users\\George Desktop\\Documents\\Stock System\\Stock Programs\\Stock Program Data\\LongStockTesting.txt")
                      
                          filecount = 0
                      
                          Do Until LongFile.Peek = -1
                              LongFile.ReadLine()
                              filecount = filecount + 1
                          Loop
                      
                          For counter = 0 To (filecount - 1)
                              stocksymbolarrayLong(counter) = Val(LongFile.ReadLine)
                          Next
                      
                          LongFile.Close()
                      
                          ReDim Preserve stocksymbolarrayLong(filecount - 1)
                      
                          ' Sort the array's
                      
                          Array.Sort(stocksymbolarrayLong)
                      

                      This should be very simple to do and solve, but i've been looking at it for a couple hours and don't know what I'm doing wrong? Can someone help?

                      George

                      F Offline
                      F Offline
                      frostcox
                      wrote on last edited by
                      #16

                      Hey I'd uses a streamreader. so store your csv file in a variable and read it in a using statement using streamreader and set your array = reader.split(',') or ('/t') depending on what format your csv file is in.

                      G 1 Reply Last reply
                      0
                      • F frostcox

                        Hey I'd uses a streamreader. so store your csv file in a variable and read it in a using statement using streamreader and set your array = reader.split(',') or ('/t') depending on what format your csv file is in.

                        G Offline
                        G Offline
                        GeorgieMPorgie
                        wrote on last edited by
                        #17

                        Thanks for the feedback.

                        George

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups