C
C#2y ago
Grin

✅ 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.
// Open the file and get a StreamReader object.
inputFile = File.OpenText("D:\\Projects\\Chapter7\\Ch7-BinarySearch\\Ch7-BinarySearch\\Names.txt");

//getting the total number of names within the file
while (!inputFile.EndOfStream)
index++;

//do I need to close the file and reopen it to prevent an error in the next while loop here?

// While we're not at the end of the array and not at the end of the file, Read the file's contents into the array.

int counter = 0;
while (counter < index && !inputFile.EndOfStream)
{
namesArray[counter] = inputFile.ReadLine();
counter++;
}

// Close the file.
inputFile.Close();
// Open the file and get a StreamReader object.
inputFile = File.OpenText("D:\\Projects\\Chapter7\\Ch7-BinarySearch\\Ch7-BinarySearch\\Names.txt");

//getting the total number of names within the file
while (!inputFile.EndOfStream)
index++;

//do I need to close the file and reopen it to prevent an error in the next while loop here?

// While we're not at the end of the array and not at the end of the file, Read the file's contents into the array.

int counter = 0;
while (counter < index && !inputFile.EndOfStream)
{
namesArray[counter] = inputFile.ReadLine();
counter++;
}

// Close the file.
inputFile.Close();
12 Replies
Grin
GrinOP2y ago
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
phaseshift
phaseshift2y ago
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
Grin
GrinOP2y ago
My book hasn't covered File.ReadAllLines How would I include that in my code?
hiyosilver
hiyosilver2y ago
ReadAllLines returns a string array. You could assign it to namesArray directly rather than going line by line.
Grin
GrinOP2y ago
I get an error when I try that
// Open the file and get a StreamReader object.
inputFile = File.OpenText("D:\\Projects\\Chapter7\\Ch7-BinarySearch\\Ch7-BinarySearch\\Names.txt");

string[] readText = File.ReadAllLines(inputFile);
// Open the file and get a StreamReader object.
inputFile = File.OpenText("D:\\Projects\\Chapter7\\Ch7-BinarySearch\\Ch7-BinarySearch\\Names.txt");

string[] readText = File.ReadAllLines(inputFile);
at the inputFile I get an error
hiyosilver
hiyosilver2y ago
What error? ReadAllLines takes a file path directly.
mtreit
mtreit2y ago
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
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit2y ago
I believe that code will not even compile.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Grin
GrinOP2y ago
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
Accord
Accord2y ago
Closed!
Want results from more Discord servers?
Add your server