Reading the contents of a file into an array
-
Thanks a lot for the feedback (and education). This is so much easier than what I was doing. :thumbsup:
George
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
-
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
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
-
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:
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
-
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
-
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
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
LoopLongFile.Close()
' Sort the array's
If Not stocksymbolarrayLong Is Nothing Then
Array.Sort(stocksymbolarrayLong)
End IfI 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!
-
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
LoopLongFile.Close()
' Sort the array's
If Not stocksymbolarrayLong Is Nothing Then
Array.Sort(stocksymbolarrayLong)
End IfI 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!
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
-
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
-
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
-
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. :)
Thanks. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
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.CSVThis 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
-
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.
Thanks for the feedback.
George