✅ Whenever I read from a file, do I need to close the file if I want to read from it again?
I want to read from a file to get the number of names within the file so I can create an array with a correct number of elements. I'm wondering if it's okay for me to not close the file before determining that number of names.
12 Replies
I need to clean up that code
I'm just curious if you need to close a file if you want to read from the start of the file twice
If you have a stream I bet you can seek back to the beginning.
I'm wondering why you don't just File.ReadAllLines though
My book hasn't covered File.ReadAllLines
How would I include that in my code?
ReadAllLines returns a string array. You could assign it to namesArray directly rather than going line by line.
I get an error when I try that
at the inputFile I get an error
What error?
ReadAllLines takes a file path directly.
Probably that it's held in use because of the first line...
Er...
Wait...
Passing the stream to ReadAllLines doesn't make any sense...
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I believe that code will not even compile.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I was just learning about binary searches in the end. But I would like to understand ReadAllLines, but I'll make a new project and continue testing for it there. I'm gonna close this for now
Closed!