Click to See Complete Forum and Search --> : How to start reading from the begining of a file for a second time?
jmcg9b
October 21st, 2004, 08:43 PM
Hi,
I am creating a StreamReader to read a file...
first i read each line to count how many there are. then i create an array based on how many lines there are but then i want to read the file from the begining again...
How can i do this? I'm guessing i can close the file and then reopen it but is there a better way? Seems a simple task to me but i cant find a method to do such...
StreamReader sr = new StreamReader(filePath);
while(sr.ReadLine() != null) //count the stock
iCount++;
StockItems = new sStockItem[iCount]; // create the stoc list
//howto restart?
String aLine;
int i = 0;
while((aLine = sr.ReadLine()) != null) //fill the stock list
{
//read data from the start of the file again...
}
sr.Close();
darwen
October 22nd, 2004, 02:23 AM
You don't need to do this : you can use an ArrayList and Add each line to it. This saves you having to read the file twice which is much more efficient.
You can also do
sr.BaseStream.Position = 0
to reset the position of the stream, but I'd recommend using the ArrayList instead.
Darwen.
Norfy
October 22nd, 2004, 03:49 AM
If this is still your problem: Structs in ArrayList (http://www.codeguru.com/forum/showthread.php?t=314544)
It's simple, stop insisting on using structs and use an ArrayList as originally planned.
jmcg9b
October 22nd, 2004, 09:16 AM
Thanks for you advice guys and yes it is still the same problem. I think this is a less complicated method than either
A. Use an arralist of structs an convert the array list ToArray() later...
and
B. Use an arraylist of a class
I really dont like the arraylist anymore but if this doesn't work out then i will goto classes...
BTW, what do you see is bad about reading the file twice?
darwen
October 22nd, 2004, 11:40 AM
Why read the file twice ? Theres no need for it. And bear in mind, file access is slow compared to doing things from memory.
Why don't you like array lists ? I do.
Darwen.
jmcg9b
October 22nd, 2004, 06:46 PM
I don't like them because...
I created an arraylist of structs which i need to search and edit a struct from...
using a foreach loop i cannot edit them because, as microsoft say, items in a foreach loop are "essentially read only"...
I think that converting to an array and doing a for loop is messy and the code i have now, reading the file twice is tidy imo...
Is there a way i could not read the file first to get the number of lines? I mean, can i do something like this...
struct myStruct
{
public int i;
public int x;
//+constructor
}
myStruct[] aStruct;
while((aLine == StreamReader.ReadLine) != null)
{
string[] fields = aLine.Split(';');
aStruct += new myStruct(fields[0], fields[1]);
..So i am not actually defining the size of the array, just adding each struct to it. Even doing this tho, i will be stuck when i come to my for loop as i wont know how many structs my array has, although i guess i could add a counter in the above code...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.